Java Code Examples for reactor.core.CoreSubscriber#onSubscribe()

The following examples show how to use reactor.core.CoreSubscriber#onSubscribe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MonoSubscribeOnValue.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
	T v = value;
	if (v == null) {
		ScheduledEmpty parent = new ScheduledEmpty(actual);
		actual.onSubscribe(parent);
		try {
			parent.setFuture(scheduler.schedule(parent));
		}
		catch (RejectedExecutionException ree) {
			if (parent.future != OperatorDisposables.DISPOSED) {
				actual.onError(Operators.onRejectedExecution(ree,
						actual.currentContext()));
			}
		}
	}
	else {
		actual.onSubscribe(new ScheduledScalar<>(actual, v, scheduler));
	}
}
 
Example 2
Source File: FluxBufferWhen.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super BUFFER> actual) {
	BufferWhenMainSubscriber<T, OPEN, CLOSE, BUFFER> main =
			new BufferWhenMainSubscriber<>(actual, bufferSupplier, queueSupplier, start, end);

	actual.onSubscribe(main);

	BufferWhenOpenSubscriber<OPEN> bos = new BufferWhenOpenSubscriber<>(main);
	if (main.subscribers.add(bos)) {
		start.subscribe(bos);
		return main;
	}
	else {
		return null;
	}
}
 
Example 3
Source File: FluxSubscribeOn.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super T> actual) {
	Worker worker = Objects.requireNonNull(scheduler.createWorker(),
			"The scheduler returned a null Function");

	SubscribeOnSubscriber<T> parent = new SubscribeOnSubscriber<>(source,
			actual, worker, requestOnSeparateThread);
	actual.onSubscribe(parent);

	try {
		worker.schedule(parent);
	}
	catch (RejectedExecutionException ree) {
		if (parent.s != Operators.cancelledSubscription()) {
			actual.onError(Operators.onRejectedExecution(ree, parent, null, null,
					actual.currentContext()));
		}
	}
	return null;
}
 
Example 4
Source File: FluxWindowWhen.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super Flux<T>> actual) {
	WindowWhenMainSubscriber<T, U, V> main = new WindowWhenMainSubscriber<>(actual,
			start, end, processorQueueSupplier);
	actual.onSubscribe(main);

	if (main.cancelled) {
		return null;
	}
	WindowWhenOpenSubscriber<T, U> os = new WindowWhenOpenSubscriber<>(main);

	if (WindowWhenMainSubscriber.BOUNDARY.compareAndSet(main,null, os)) {
		WindowWhenMainSubscriber.OPEN_WINDOW_COUNT.incrementAndGet(main);
		start.subscribe(os);
		return main;
	}
	else {
		return null;
	}
}
 
Example 5
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
    if (this.inner == null && INNER.compareAndSet(this, null, actual)) {
        if (this.first == null && this.done) {
            final Throwable t = this.throwable;
            if (t != null) {
                Operators.error(actual, t);
            }
            else {
                Operators.complete(actual);
            }
            return;
        }
        actual.onSubscribe(this);
    }
    else if (this.inner != Operators.EMPTY_SUBSCRIBER) {
        Operators.error(actual, new IllegalStateException("FluxSwitchOnFirst allows only one Subscriber"));
    } else {
        Operators.error(actual, new CancellationException("FluxSwitchOnFirst has already been cancelled"));
    }
}
 
Example 6
Source File: FutureMono.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public final void subscribe(final CoreSubscriber<? super Void> s) {
	if(future.isDone()){
		if(future.isSuccess()){
			Operators.complete(s);
		}
		else{
			Operators.error(s, FutureSubscription.wrapError(future.cause()));
		}
		return;
	}

	FutureSubscription<F> fs = new FutureSubscription<>(future, s);
	// Returned value is deliberately ignored
	future.addListener(fs);
	s.onSubscribe(fs);
}
 
Example 7
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
    if (this.inner == null && INNER.compareAndSet(this, null, Operators.toConditionalSubscriber(actual))) {
        if (this.first == null && this.done) {
            final Throwable t = this.throwable;
            if (t != null) {
                Operators.error(actual, t);
            }
            else {
                Operators.complete(actual);
            }
            return;
        }
        actual.onSubscribe(this);
    }
    else if (this.inner != Operators.EMPTY_SUBSCRIBER) {
        Operators.error(actual, new IllegalStateException("FluxSwitchOnFirst allows only one Subscriber"));
    } else {
        Operators.error(actual, new CancellationException("FluxSwitchOnFirst has already been cancelled"));
    }
}
 
Example 8
Source File: FluxReceive.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super Object> s) {
	if (once == 0 && ONCE.compareAndSet(this, 0, 1)) {
		if (log.isDebugEnabled()) {
			log.debug(format(channel, "Subscribing inbound receiver [pending: {}, cancelled:{}, " +
							"inboundDone: {}]"),
					getPending(),
					isCancelled(),
					inboundDone);
		}
		if (inboundDone && getPending() == 0) {
			if (inboundError != null) {
				Operators.error(s, inboundError);
				return;
			}

			Operators.complete(s);
			return;
		}

		receiver = s;

		s.onSubscribe(this);
	}
	else {
		if (inboundDone && getPending() == 0) {
			if (inboundError != null) {
				Operators.error(s, inboundError);
				return;
			}

			Operators.complete(s);
		}
		else {
			Operators.error(s,
					new IllegalStateException(
							"Only one connection receive subscriber allowed."));
		}
	}
}
 
Example 9
Source File: MonoRetry.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super T> actual) {
	FluxRetry.RetrySubscriber<T> parent = new FluxRetry.RetrySubscriber<>(source,
			actual, times);

	actual.onSubscribe(parent);

	if (!parent.isCancelled()) {
		parent.resubscribe();
	}
	return null;
}
 
Example 10
Source File: MonoZip.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void subscribe(CoreSubscriber<? super R> actual) {
	Publisher<?>[] a;
	int n = 0;
	if (sources != null) {
		a = sources;
		n = a.length;
	}
	else {
		a = new Publisher[8];
		for (Publisher<?> m : sourcesIterable) {
			if (n == a.length) {
				Publisher<?>[] b = new Publisher[n + (n >> 2)];
				System.arraycopy(a, 0, b, 0, n);
				a = b;
			}
			a[n++] = m;
		}
	}

	if (n == 0) {
		Operators.complete(actual);
		return;
	}

	ZipCoordinator<R> parent = new ZipCoordinator<>(actual, n, delayError, zipper);
	actual.onSubscribe(parent);
	ZipInner<R>[] subs = parent.subscribers;
	for (int i = 0; i < n; i++) {
		a[i].subscribe(subs[i]);
	}
}
 
Example 11
Source File: MonoFlatMap.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super R> actual) {
	//for now Mono in general doesn't support onErrorContinue, so the scalar version shouldn't either
	if (FluxFlatMap.trySubscribeScalarMap(source, actual, mapper, true, false)) {
		return null;
	}

	FlatMapMain<T, R> manager = new FlatMapMain<>(actual, mapper);
	actual.onSubscribe(manager);

	return manager;
}
 
Example 12
Source File: MonoWhen.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void subscribe(CoreSubscriber<? super Void> actual) {
	Publisher<?>[] a;
	int n = 0;
	if (sources != null) {
		a = sources;
		n = a.length;
	}
	else {
		a = new Publisher[8];
		for (Publisher<?> m : sourcesIterable) {
			if (n == a.length) {
				Publisher<?>[] b = new Publisher[n + (n >> 2)];
				System.arraycopy(a, 0, b, 0, n);
				a = b;
			}
			a[n++] = m;
		}
	}

	if (n == 0) {
		Operators.complete(actual);
		return;
	}

	WhenCoordinator parent = new WhenCoordinator(actual, n, delayError);
	actual.onSubscribe(parent);
	parent.subscribe(a);
}
 
Example 13
Source File: MonoSubscribeOnCallable.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
	FluxSubscribeOnCallable.CallableSubscribeOnSubscription<T> parent =
			new FluxSubscribeOnCallable.CallableSubscribeOnSubscription<>(actual, callable, scheduler);
	actual.onSubscribe(parent);

	try {
		parent.setMainFuture(scheduler.schedule(parent));
	}
	catch (RejectedExecutionException ree) {
		if(parent.state != FluxSubscribeOnCallable.CallableSubscribeOnSubscription.HAS_CANCELLED) {
			actual.onError(Operators.onRejectedExecution(ree, actual.currentContext()));
		}
	}
}
 
Example 14
Source File: MonoRetryPredicate.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super T> actual) {

	FluxRetryPredicate.RetryPredicateSubscriber<T> parent =
			new FluxRetryPredicate.RetryPredicateSubscriber<>(source,
					actual, predicate);

	actual.onSubscribe(parent);

	if (!parent.isCancelled()) {
		parent.resubscribe();
	}
	return null;
}
 
Example 15
Source File: ParallelThenTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void scanMainSubscriberError() {
	CoreSubscriber<? super Void> subscriber = new LambdaSubscriber<>(null, e -> { }, null,
			sub -> sub.request(2));
	ParallelThen.ThenMain test = new ParallelThen.ThenMain(subscriber, 2);

	subscriber.onSubscribe(test);

	assertThat(test.scan(Scannable.Attr.ERROR)).isNull();
	test.innerError(new IllegalStateException("boom"));
	assertThat(test.scan(Scannable.Attr.ERROR)).hasMessage("boom");
}
 
Example 16
Source File: SubscribeOnlyOnceLifter.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public CoreSubscriber<? super T> apply(Scannable scannable, CoreSubscriber<? super T> coreSubscriber) {
    return new CoreSubscriber<T>() {
        @Override
        public void onSubscribe(Subscription subscription) {
            if (!compareAndSet(false, true)) {
                throw new NullPointerException("You cannot directly subscribe to a gRPC service multiple times " +
                        "concurrently. Use Flux.share() instead.");
            } else {
                coreSubscriber.onSubscribe(subscription);
            }
        }

        @Override
        public void onNext(T t) {
            coreSubscriber.onNext(t);
        }

        @Override
        public void onError(Throwable throwable) {
            coreSubscriber.onError(throwable);
        }

        @Override
        public void onComplete() {
            coreSubscriber.onComplete();
        }
    };
}
 
Example 17
Source File: MonoCacheTime.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super T> actual) {
	CacheMonoSubscriber<T> inner = new CacheMonoSubscriber<>(actual);
	actual.onSubscribe(inner);
	for(;;){
		Signal<T> state = this.state;
		if (state == EMPTY || state instanceof CoordinatorSubscriber) {
			boolean subscribe = false;
			CoordinatorSubscriber<T> coordinator;
			if (state == EMPTY) {
				coordinator = new CoordinatorSubscriber<>(this);
				if (!STATE.compareAndSet(this, EMPTY, coordinator)) {
					continue;
				}
				subscribe = true;
			}
			else {
				coordinator = (CoordinatorSubscriber<T>) state;
			}

			if (coordinator.add(inner)) {
				if (inner.isCancelled()) {
					coordinator.remove(inner);
				}
				else {
					inner.coordinator = coordinator;
				}

				if (subscribe) {
					source.subscribe(coordinator);
				}
				break;
			}
		}
		else {
			//state is an actual signal, cached
			if (state.isOnNext()) {
				inner.complete(state.get());
			}
			else if (state.isOnComplete()) {
				inner.onComplete();
			}
			else {
				inner.onError(state.getThrowable());
			}
			break;
		}
	}
	return null;
}
 
Example 18
Source File: SimplePool.java    From reactor-pool with Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super PooledRef<T>> actual) {
    Objects.requireNonNull(actual, "subscribing with null");
    Borrower<T> borrower = new Borrower<>(actual, parent, acquireTimeout);
    actual.onSubscribe(borrower);
}
 
Example 19
Source File: FluxTimeout.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super T> actual) {
	CoreSubscriber<T> serial = Operators.serialize(actual);

	TimeoutMainSubscriber<T, V> main =
			new TimeoutMainSubscriber<>(serial, itemTimeout, other, timeoutDescription);

	serial.onSubscribe(main);

	TimeoutTimeoutSubscriber ts = new TimeoutTimeoutSubscriber(main, 0L);

	main.setTimeout(ts);

	firstTimeout.subscribe(ts);

	return main;
}
 
Example 20
Source File: Operators.java    From reactor-core with Apache License 2.0 3 votes vote down vote up
/**
 * Report a {@link Throwable} that was thrown from a call to {@link Publisher#subscribe(Subscriber)},
 * attempting to notify the {@link Subscriber} by:
 * <ol>
 *     <li>providing a special {@link Subscription} via {@link Subscriber#onSubscribe(Subscription)}</li>
 *     <li>immediately delivering an {@link Subscriber#onError(Throwable) onError} signal after that</li>
 * </ol>
 * <p>
 * As at that point the subscriber MAY have already been provided with a {@link Subscription}, we
 * assume most well formed subscribers will ignore this second {@link Subscription} per Reactive
 * Streams rule 1.9. Subscribers that don't usually ignore may recognize this special case and ignore
 * it by checking {@link #canAppearAfterOnSubscribe(Subscription)}.
 * <p>
 * Note that if the {@link Subscriber#onSubscribe(Subscription) onSubscribe} attempt throws,
 * {@link Exceptions#throwIfFatal(Throwable) fatal} exceptions are thrown. Other exceptions
 * are added as {@link Throwable#addSuppressed(Throwable) suppressed} on the original exception,
 * which is then directly notified as an {@link Subscriber#onError(Throwable) onError} signal
 * (again assuming that such exceptions occur because a {@link Subscription} is already set).
 *
 * @param subscriber the {@link Subscriber} being subscribed when the error happened
 * @param e the {@link Throwable} that was thrown from {@link Publisher#subscribe(Subscriber)}
 * @see #canAppearAfterOnSubscribe(Subscription)
 */
public static void reportThrowInSubscribe(CoreSubscriber<?> subscriber, Throwable e) {
	try {
		subscriber.onSubscribe(EmptySubscription.FROM_SUBSCRIBE_INSTANCE);
	}
	catch (Throwable onSubscribeError) {
		Exceptions.throwIfFatal(onSubscribeError);
		e.addSuppressed(onSubscribeError);
	}
	subscriber.onError(onOperatorError(e, subscriber.currentContext()));
}