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

The following examples show how to use reactor.core.CoreSubscriber#onNext() . 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: FluxConcatMapTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void discardOnNextQueueReject() {
	List<Object> discarded = new ArrayList<>();
	AssertSubscriber<Object> discardSubscriber = new AssertSubscriber<>(
			Context.of(Hooks.KEY_ON_DISCARD, (Consumer<?>) discarded::add));

	final CoreSubscriber<Object> subscriber =
			FluxConcatMap.subscriber(discardSubscriber,
					Mono::just,
					Queues.get(0),
					1,
					FluxConcatMap.ErrorMode.IMMEDIATE);
	subscriber.onSubscribe(Operators.emptySubscription());

	subscriber.onNext(1);

	assertThat(discarded).containsExactly(1);
}
 
Example 2
Source File: FluxConcatMapTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void discardDelayedOnNextQueueReject() {
	List<Object> discarded = new ArrayList<>();
	AssertSubscriber<Object> testSubscriber = new AssertSubscriber<>(
			Context.of(Hooks.KEY_ON_DISCARD, (Consumer<?>) discarded::add));
	final CoreSubscriber<Object> subscriber =
			FluxConcatMap.subscriber(testSubscriber,
					Mono::just,
					Queues.get(0),
					1,
					FluxConcatMap.ErrorMode.END);
	subscriber.onSubscribe(Operators.emptySubscription());

	subscriber.onNext(1);

	assertThat(discarded).containsExactly(1);
}
 
Example 3
Source File: HooksTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
private static CoreSubscriber<Object> liftSubscriber(Scannable scannable, CoreSubscriber<? super Object> sub, AtomicInteger liftCounter) {
	liftCounter.incrementAndGet();
	return new CoreSubscriber<Object>() {

		@Override
		public void onSubscribe(Subscription s) {
			sub.onSubscribe(s);
		}

		@Override
		public void onNext(Object o) {
			System.out.println("Lifting " + o + " out of " + scannable.stepName());
			sub.onNext((Integer) o + 100);
		}

		@Override
		public void onError(Throwable t) {
			sub.onError(t);
		}

		@Override
		public void onComplete() {
			sub.onComplete();
		}
	};
}
 
Example 4
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 5
Source File: TransportConnector.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
void _subscribe(CoreSubscriber<? super Channel> actual) {
	this.actual = actual;
	actual.onSubscribe(this);

	if (isDone()) {
		if (isSuccess()) {
			actual.onNext(channel);
			actual.onComplete();
		}
		else {
			actual.onError(cause());
		}
	}
}
 
Example 6
Source File: FluxCombineLatest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
void drainOutput() {
	final CoreSubscriber<? super R> a = actual;
	final Queue<SourceAndArray> q = queue;

	int missed = 1;

	for (; ; ) {

		if (cancelled) {
			discardQueue(q);
			return;
		}

		Throwable ex = error;
		if (ex != null) {
			discardQueue(q);
			a.onError(ex);
			return;
		}

		boolean d = done;

		boolean empty = q.isEmpty();

		if (!empty) {
			a.onNext(null);
		}

		if (d && empty) {
			a.onComplete();
			return;
		}

		missed = WIP.addAndGet(this, -missed);
		if (missed == 0) {
			break;
		}
	}
}
 
Example 7
Source File: UnicastProcessor.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
void drainFused(CoreSubscriber<? super T> a) {
	int missed = 1;

	final Queue<T> q = queue;

	for (;;) {

		if (cancelled) {
			// We are the holder of the queue, but we still have to perform discarding under the guarded block
			// to prevent any racing done by downstream
			this.clear();
			hasDownstream = false;
			return;
		}

		boolean d = done;

		a.onNext(null);

		if (d) {
			hasDownstream = false;

			Throwable ex = error;
			if (ex != null) {
				a.onError(ex);
			} else {
				a.onComplete();
			}
			return;
		}

		missed = WIP.addAndGet(this, -missed);
		if (missed == 0) {
			break;
		}
	}
}
 
Example 8
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onNext(T t) {
    final CoreSubscriber<? super T> i = this.inner;
    if (this.done || i == Operators.EMPTY_SUBSCRIBER) {
        Operators.onNextDropped(t, currentContext());
        return;
    }

    if (i == null) {
        final Publisher<? extends R> result;
        final CoreSubscriber<? super R> o = this.outer;

        try {
            result = Objects.requireNonNull(
                this.transformer.apply(Signal.next(t, o.currentContext()), this),
                "The transformer returned a null value"
            );
        }
        catch (Throwable e) {
            this.done = true;
            Operators.error(o, Operators.onOperatorError(this.s, e, t, o.currentContext()));
            return;
        }

        this.first = t;
        result.subscribe(o);
        return;
    }

    i.onNext(t);
}
 
Example 9
Source File: UnicastProcessor.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
void drainRegular(CoreSubscriber<? super T> a) {
	int missed = 1;

	final Queue<T> q = queue;

	for (;;) {

		long r = requested;
		long e = 0L;

		while (r != e) {
			boolean d = done;

			T t = q.poll();
			boolean empty = t == null;

			if (checkTerminated(d, empty, a, q, t)) {
				return;
			}

			if (empty) {
				break;
			}

			a.onNext(t);

			e++;
		}

		if (r == e) {
			if (checkTerminated(done, q.isEmpty(), a, q, null)) {
				return;
			}
		}

		if (e != 0 && r != Long.MAX_VALUE) {
			REQUESTED.addAndGet(this, -e);
		}

		missed = WIP.addAndGet(this, -missed);
		if (missed == 0) {
			break;
		}
	}
}
 
Example 10
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
boolean tryOnNext(CoreSubscriber<? super T> actual, T t) {
    actual.onNext(t);
    return true;
}
 
Example 11
Source File: SerializedSubscriber.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
void serDrainLoop(CoreSubscriber<? super T> actual) {
	for (; ; ) {

		if (cancelled) {
			synchronized (this) {
				discardMultiple(this.head);
			}
			return;
		}

		boolean d;
		Throwable e;
		LinkedArrayNode<T> n;

		synchronized (this) {
			if (cancelled) {
				discardMultiple(this.head);
				return;
			}

			if (!concurrentlyAddedContent) {
				drainLoopInProgress = false;
				return;
			}

			concurrentlyAddedContent = false;

			d = done;
			e = error;
			n = head;

			head = null;
			tail = null;
		}

		while (n != null) {

			T[] arr = n.array;
			int c = n.count;

			for (int i = 0; i < c; i++) {

				if (cancelled) {
					synchronized (this) {
						discardMultiple(n);
					}
					return;
				}

				actual.onNext(arr[i]);
			}

			n = n.next;
		}

		if (cancelled) {
			synchronized (this) {
				discardMultiple(this.head);
			}
			return;
		}

		if (e != null) {
			actual.onError(e);
			return;
		}
		else if (d) {
			actual.onComplete();
			return;
		}
	}
}