All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reactor.core.publisher.FluxDoFinally Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/*
 * Copyright (c) 2016-2022 VMware Inc. or its affiliates, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package reactor.core.publisher;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Consumer;

import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.Exceptions;
import reactor.core.Fuseable;
import reactor.core.Fuseable.ConditionalSubscriber;
import reactor.core.Fuseable.QueueSubscription;
import reactor.util.annotation.Nullable;

/**
 * Hook into the lifecycle events and signals of a {@link Flux} and execute
 * a provided callback after any of onComplete, onError and cancel events.
 * The hook is executed only once and receives the event type that triggered
 * it ({@link SignalType#ON_COMPLETE}, {@link SignalType#ON_ERROR} or
 * {@link SignalType#CANCEL}).
 * 

* Note that any exception thrown by the hook are caught and bubbled up * using {@link Operators#onErrorDropped(Throwable, reactor.util.context.Context)}. * * @param the value type * @author Simon Baslé */ final class FluxDoFinally extends InternalFluxOperator { final Consumer onFinally; @SuppressWarnings("unchecked") static CoreSubscriber createSubscriber(CoreSubscriber s, Consumer onFinally) { if (s instanceof ConditionalSubscriber) { return new DoFinallyConditionalSubscriber<>((ConditionalSubscriber) s, onFinally); } return new DoFinallySubscriber<>(s, onFinally); } FluxDoFinally(Flux source, Consumer onFinally) { super(source); this.onFinally = onFinally; } @Override public CoreSubscriber subscribeOrReturn(CoreSubscriber actual) { return createSubscriber(actual, onFinally); } @Override public Object scanUnsafe(Attr key) { if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC; return super.scanUnsafe(key); } static class DoFinallySubscriber implements InnerOperator { final CoreSubscriber actual; final Consumer onFinally; volatile int once; @SuppressWarnings("rawtypes") static final AtomicIntegerFieldUpdater ONCE = AtomicIntegerFieldUpdater.newUpdater(DoFinallySubscriber.class, "once"); Subscription s; DoFinallySubscriber(CoreSubscriber actual, Consumer onFinally) { this.actual = actual; this.onFinally = onFinally; } @Override @Nullable public Object scanUnsafe(Attr key) { if (key == Attr.PARENT) return s; if (key == Attr.TERMINATED || key == Attr.CANCELLED) return once == 1; if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC; return InnerOperator.super.scanUnsafe(key); } @Override public void onSubscribe(Subscription s) { if (Operators.validate(this.s, s)) { this.s = s; actual.onSubscribe(this); } } @Override public void onNext(T t) { actual.onNext(t); } @Override public void onError(Throwable t) { try { actual.onError(t); } finally { runFinally(SignalType.ON_ERROR); } } @Override public void onComplete() { actual.onComplete(); runFinally(SignalType.ON_COMPLETE); } @Override public void cancel() { s.cancel(); runFinally(SignalType.CANCEL); } @Override public void request(long n) { s.request(n); } void runFinally(SignalType signalType) { if (ONCE.compareAndSet(this, 0, 1)) { try { onFinally.accept(signalType); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); Operators.onErrorDropped(ex, actual.currentContext()); } } } @Override public CoreSubscriber actual() { return actual; } } static final class DoFinallyConditionalSubscriber extends DoFinallySubscriber implements ConditionalSubscriber { DoFinallyConditionalSubscriber(ConditionalSubscriber actual, Consumer onFinally) { super(actual, onFinally); } @Override @SuppressWarnings("unchecked") public boolean tryOnNext(T t) { return ((ConditionalSubscriber)actual).tryOnNext(t); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy