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

reactor.core.publisher.FluxUsing 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-2023 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.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Consumer;
import java.util.function.Function;

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

/**
 * Uses a resource, generated by a supplier for each individual Subscriber,
 * while streaming the values from a
 * Publisher derived from the same resource and makes sure the resource is released
 * if the sequence terminates or the Subscriber cancels.
 * 

*

* Eager resource cleanup happens just before the source termination and exceptions * raised by the cleanup Consumer may override the terminal event. Non-eager * cleanup will drop any exception. * * @param the value type streamed * @param the resource type * * @see Reactive-Streams-Commons */ final class FluxUsing extends Flux implements Fuseable, SourceProducer { final Callable resourceSupplier; final Function> sourceFactory; final Consumer resourceCleanup; final boolean eager; FluxUsing(Callable resourceSupplier, Function> sourceFactory, Consumer resourceCleanup, boolean eager) { this.resourceSupplier = Objects.requireNonNull(resourceSupplier, "resourceSupplier"); this.sourceFactory = Objects.requireNonNull(sourceFactory, "sourceFactory"); this.resourceCleanup = Objects.requireNonNull(resourceCleanup, "resourceCleanup"); this.eager = eager; } @Override @SuppressWarnings("unchecked") public void subscribe(CoreSubscriber actual) { S resource; try { resource = resourceSupplier.call(); } catch (Throwable e) { Operators.error(actual, Operators.onOperatorError(e, actual.currentContext())); return; } Publisher p; try { p = Objects.requireNonNull(sourceFactory.apply(resource), "The sourceFactory returned a null value"); } catch (Throwable e) { Throwable _e = Operators.onOperatorError(e, actual.currentContext()); try { resourceCleanup.accept(resource); } catch (Throwable ex) { _e = Exceptions.addSuppressed(ex, _e); } Operators.error(actual, _e); return; } if (p instanceof Fuseable) { from(p).subscribe(new UsingFuseableSubscriber<>(actual, resourceCleanup, resource, eager)); } else if (actual instanceof ConditionalSubscriber) { from(p).subscribe(new UsingConditionalSubscriber<>((ConditionalSubscriber) actual, resourceCleanup, resource, eager)); } else { from(p).subscribe(new UsingSubscriber<>(actual, resourceCleanup, resource, eager)); } } @Override public Object scanUnsafe(Attr key) { if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC; return SourceProducer.super.scanUnsafe(key); } static final class UsingSubscriber implements InnerOperator, QueueSubscription { final CoreSubscriber actual; final Consumer resourceCleanup; final S resource; final boolean eager; Subscription s; volatile int wip; @SuppressWarnings("rawtypes") static final AtomicIntegerFieldUpdater WIP = AtomicIntegerFieldUpdater.newUpdater(UsingSubscriber.class, "wip"); UsingSubscriber(CoreSubscriber actual, Consumer resourceCleanup, S resource, boolean eager) { this.actual = actual; this.resourceCleanup = resourceCleanup; this.resource = resource; this.eager = eager; } @Override @Nullable public Object scanUnsafe(Attr key) { if (key == Attr.TERMINATED || key == Attr.CANCELLED) return wip == 1; if (key == Attr.PARENT) return s; if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC; return InnerOperator.super.scanUnsafe(key); } @Override public CoreSubscriber actual() { return actual; } @Override public void request(long n) { s.request(n); } @Override public void cancel() { if (WIP.compareAndSet(this, 0, 1)) { s.cancel(); cleanup(); } } void cleanup() { try { resourceCleanup.accept(resource); } catch (Throwable e) { Operators.onErrorDropped(e, actual.currentContext()); } } @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) { if (eager && WIP.compareAndSet(this, 0, 1)) { try { resourceCleanup.accept(resource); } catch (Throwable e) { Throwable _e = Operators.onOperatorError(e, actual.currentContext()); t = Exceptions.addSuppressed(_e, t); } } actual.onError(t); if (!eager && WIP.compareAndSet(this, 0, 1)) { cleanup(); } } @Override public void onComplete() { if (eager && WIP.compareAndSet(this, 0, 1)) { try { resourceCleanup.accept(resource); } catch (Throwable e) { actual.onError(Operators.onOperatorError(e, actual.currentContext())); return; } } actual.onComplete(); if (!eager && WIP.compareAndSet(this, 0, 1)) { cleanup(); } } @Override public int requestFusion(int requestedMode) { return NONE; // always reject, upstream turned out to be non-fuseable after all } @Override public void clear() { // ignoring fusion methods } @Override public boolean isEmpty() { // ignoring fusion methods return true; } @Override @Nullable public T poll() { return null; } @Override public int size() { return 0; } } static final class UsingFuseableSubscriber implements InnerOperator, QueueSubscription { final CoreSubscriber actual; final Consumer resourceCleanup; final S resource; final boolean eager; QueueSubscription s; volatile int wip; @SuppressWarnings("rawtypes") static final AtomicIntegerFieldUpdater WIP = AtomicIntegerFieldUpdater.newUpdater(UsingFuseableSubscriber.class, "wip"); int mode; UsingFuseableSubscriber(CoreSubscriber actual, Consumer resourceCleanup, S resource, boolean eager) { this.actual = actual; this.resourceCleanup = resourceCleanup; this.resource = resource; this.eager = eager; } @Override @Nullable public Object scanUnsafe(Attr key) { if (key == Attr.TERMINATED || key == Attr.CANCELLED) return wip == 1; if (key == Attr.PARENT) return s; if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC; return InnerOperator.super.scanUnsafe(key); } @Override public CoreSubscriber actual() { return actual; } @Override public void request(long n) { s.request(n); } @Override public void cancel() { if (WIP.compareAndSet(this, 0, 1)) { s.cancel(); cleanup(); } } void cleanup() { try { resourceCleanup.accept(resource); } catch (Throwable e) { Operators.onErrorDropped(e, actual.currentContext()); } } @SuppressWarnings("unchecked") @Override public void onSubscribe(Subscription s) { if (Operators.validate(this.s, s)) { this.s = (QueueSubscription) s; actual.onSubscribe(this); } } @Override public void onNext(T t) { actual.onNext(t); } @Override public void onError(Throwable t) { if (eager && WIP.compareAndSet(this, 0, 1)) { try { resourceCleanup.accept(resource); } catch (Throwable e) { Throwable _e = Operators.onOperatorError(e, actual.currentContext()); t = Exceptions.addSuppressed(_e, t); } } actual.onError(t); if (!eager && WIP.compareAndSet(this, 0, 1)) { cleanup(); } } @Override public void onComplete() { if (eager && WIP.compareAndSet(this, 0, 1)) { try { resourceCleanup.accept(resource); } catch (Throwable e) { actual.onError(Operators.onOperatorError(e, actual.currentContext())); return; } } actual.onComplete(); if (!eager && WIP.compareAndSet(this, 0, 1)) { cleanup(); } } @Override public void clear() { s.clear(); } @Override public boolean isEmpty() { return s.isEmpty(); } @Override @Nullable public T poll() { T v = s.poll(); if (v == null && mode == SYNC) { if (WIP.compareAndSet(this, 0, 1)) { resourceCleanup.accept(resource); } } return v; } @Override public int requestFusion(int requestedMode) { int m = s.requestFusion(requestedMode); mode = m; return m; } @Override public int size() { return s.size(); } } static final class UsingConditionalSubscriber implements ConditionalSubscriber, InnerOperator, QueueSubscription { final ConditionalSubscriber actual; final Consumer resourceCleanup; final S resource; final boolean eager; Subscription s; volatile int wip; @SuppressWarnings("rawtypes") static final AtomicIntegerFieldUpdater WIP = AtomicIntegerFieldUpdater.newUpdater(UsingConditionalSubscriber.class, "wip"); UsingConditionalSubscriber(ConditionalSubscriber actual, Consumer resourceCleanup, S resource, boolean eager) { this.actual = actual; this.resourceCleanup = resourceCleanup; this.resource = resource; this.eager = eager; } @Override @Nullable public Object scanUnsafe(Attr key) { if (key == Attr.TERMINATED || key == Attr.CANCELLED) return wip == 1; if (key == Attr.PARENT) return s; if (key == Attr.RUN_STYLE) return Attr.RunStyle.SYNC; return InnerOperator.super.scanUnsafe(key); } @Override public CoreSubscriber actual() { return actual; } @Override public void request(long n) { s.request(n); } @Override public void cancel() { if (WIP.compareAndSet(this, 0, 1)) { s.cancel(); cleanup(); } } void cleanup() { try { resourceCleanup.accept(resource); } catch (Throwable e) { Operators.onErrorDropped(e, actual.currentContext()); } } @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 boolean tryOnNext(T t) { return actual.tryOnNext(t); } @Override public void onError(Throwable t) { if (eager && WIP.compareAndSet(this, 0, 1)) { try { resourceCleanup.accept(resource); } catch (Throwable e) { Throwable _e = Operators.onOperatorError(e, actual.currentContext()); t = Exceptions.addSuppressed(_e, t); } } actual.onError(t); if (!eager && WIP.compareAndSet(this, 0, 1)) { cleanup(); } } @Override public void onComplete() { if (eager && WIP.compareAndSet(this, 0, 1)) { try { resourceCleanup.accept(resource); } catch (Throwable e) { actual.onError(Operators.onOperatorError(e, actual.currentContext())); return; } } actual.onComplete(); if (!eager && WIP.compareAndSet(this, 0, 1)) { cleanup(); } } @Override public int requestFusion(int requestedMode) { return NONE; // always reject, upstream turned out to be non-fuseable after all } @Override public void clear() { // ignoring fusion methods } @Override public boolean isEmpty() { // ignoring fusion methods return true; } @Override @Nullable public T poll() { return null; } @Override public int size() { return 0; } } }