Java Code Examples for org.reactivestreams.Subscriber#onNext()

The following examples show how to use org.reactivestreams.Subscriber#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: FlowableRange.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Override
void fastPath() {
    int f = end;
    Subscriber<? super Integer> a = actual;

    for (int i = index; i != f; i++) {
        if (cancelled) {
            return;
        }
        a.onNext(i);
    }
    if (cancelled) {
        return;
    }
    a.onComplete();
}
 
Example 2
Source File: SerializedProcessor.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
/**
 * Dispatches the events contained in the queue to the given subscriber.
 *
 * @param queue the queue of event
 * @param subscriber the subscriber to emit the events to
 */
@SuppressWarnings("unchecked")
public void dispatch(List<Object> queue, Subscriber<I> subscriber) {
    for (Object event : queue) {
        if (event != null) {
            if (event instanceof SerializedProcessor.SubscriptionEvent) {
                subscriber.onSubscribe(((SubscriptionEvent) event).subscription);
            }

            if (event instanceof SerializedProcessor.FailureEvent) {
                subscriber.onError(((FailureEvent) event).failure);
                return;
            }

            if (event instanceof SerializedProcessor.CompletionEvent) {
                subscriber.onComplete();
                return;
            }

            if (event instanceof SerializedProcessor.ItemEvent) {
                subscriber.onNext(((ItemEvent<I>) event).item);
            }
        }
    }
}
 
Example 3
Source File: KafkaSink.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private void requestNext(Message<?> message) {
    Subscriber<? super Message<?>> down = downstream.get();
    if (down != null) {
        down.onNext(message);
    }
    Subscription up = this.subscription.get();
    if (up != null) {
        up.request(1);
    }
}
 
Example 4
Source File: SubscribeOnlyOnceFlowableOperator.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Subscriber<? super T> apply(final Subscriber<? super T> observer) {
    return new Subscriber<T>() {
        @Override
        public void onSubscribe(Subscription subscription) {
            if (subscribedOnce.getAndSet(true)) {
                throw new NullPointerException("You cannot directly subscribe to a gRPC service multiple times " +
                        "concurrently. Use Flowable.share() instead.");
            } else {
                observer.onSubscribe(subscription);
            }
        }

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

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

        @Override
        public void onComplete() {
            observer.onComplete();
        }
    };
}
 
Example 5
Source File: FlowControllingHttpContentProducer.java    From styx with Apache License 2.0 5 votes vote down vote up
private void emitChunks(Subscriber<? super ByteBuf> downstream) {
    LongUnaryOperator decrementIfBackpressureEnabled = current -> current == Long.MAX_VALUE ? current : current > 0 ? current - 1 : 0;
    LongUnaryOperator incrementIfBackpressureEnabled = current -> current == Long.MAX_VALUE ? current : current + 1;

    while (requested.getAndUpdate(decrementIfBackpressureEnabled) > 0) {
        ByteBuf value = this.readQueue.poll();
        if (value == null) {
            requested.getAndUpdate(incrementIfBackpressureEnabled);
            break;
        }
        emittedBytes.addAndGet(value.readableBytes());
        emittedChunks.incrementAndGet();
        downstream.onNext(value);
    }
}
 
Example 6
Source File: ParallelInvalid.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(Subscriber<? super Object>[] subscribers) {
    TestException ex = new TestException();
    for (Subscriber<? super Object> s : subscribers) {
        EmptySubscription.error(ex, s);
        s.onError(ex);
        s.onNext(0);
        s.onComplete();
        s.onComplete();
    }
}
 
Example 7
Source File: FlowableRangeLong.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
@Override
void slowPath(long r) {
    long e = 0;
    long f = end;
    long i = index;
    Subscriber<? super Long> a = actual;

    for (;;) {

        while (e != r && i != f) {
            if (cancelled) {
                return;
            }

            a.onNext(i);

            e++;
            i++;
        }

        if (i == f) {
            if (!cancelled) {
                a.onComplete();
            }
            return;
        }

        r = get();
        if (e == r) {
            index = i;
            r = addAndGet(-e);
            if (r == 0L) {
                return;
            }
            e = 0L;
        }
    }
}
 
Example 8
Source File: SmartMulticastProcessor.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
void tryEmit(NewsLetter element) {
	if (done) {
		return;
	}

	int wip;

	if ((wip = WIP.incrementAndGet(this)) > 1) {
		sent = false;
		return;
	}

	Subscriber<? super  NewsLetter> a = actual;
	long r = requested;

	for (;;) {
		if (r > 0) {
			a.onNext(element.withRecipient(getRecipient()));
			sent = true;

			REQUESTED.decrementAndGet(this);
			this.wip = 0;

			return;
		}
		else {
			wip = WIP.addAndGet(this, -wip);

			if (wip == 0) {
				sent = false;
				return;
			}
			else {
				r = requested;
			}
		}
	}
}
 
Example 9
Source File: RxBusUtil.java    From RxBus2 with Apache License 2.0 5 votes vote down vote up
public static <T> Subscriber<T> wrapSubscriber(Subscriber<T> subscriber, IRxBusQueue isResumedProvider)
{
    return new Subscriber<T>()
    {
        @Override
        public void onSubscribe(Subscription s) {
            subscriber.onSubscribe(s);
        }

        @Override
        public void onComplete()
        {
            subscriber.onComplete();
        }

        @Override
        public void onError(Throwable e)
        {
            subscriber.onError(e);
        }


        @Override
        public void onNext(T t)
        {
            if (RxUtil.safetyQueueCheck(t, isResumedProvider))
                subscriber.onNext(t);
        }
    };
}
 
Example 10
Source File: RxSubscriptionUtil.java    From RxBus2 with Apache License 2.0 5 votes vote down vote up
public static <T> Subscriber<T> wrapSubscriber(Subscriber<T> subscriber, IRxBusQueue isResumedProvider)
{
    return new Subscriber<T>()
    {
        @Override
        public void onSubscribe(Subscription s) {
            subscriber.onSubscribe(s);
        }

        @Override
        public void onComplete()
        {
            subscriber.onComplete();
        }

        @Override
        public void onError(Throwable e)
        {
            subscriber.onError(e);
        }

        @Override
        public void onNext(T t)
        {
            if (RxUtil.safetyQueueCheck(t, isResumedProvider))
                subscriber.onNext(t);
        }
    };
}
 
Example 11
Source File: HalfSerializer.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
/**
 * Emits the given value if possible and terminates if there was an onComplete or onError
 * while emitting, drops the value otherwise.
 * @param <T> the value type
 * @param subscriber the target Subscriber to emit to
 * @param value the value to emit
 * @param wip the serialization work-in-progress counter/indicator
 * @param error the holder of Throwables
 */
public static <T> void onNext(Subscriber<? super T> subscriber, T value,
        AtomicInteger wip, AtomicThrowable error) {
    if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
        subscriber.onNext(value);
        if (wip.decrementAndGet() != 0) {
            Throwable ex = error.terminate();
            if (ex != null) {
                subscriber.onError(ex);
            } else {
                subscriber.onComplete();
            }
        }
    }
}
 
Example 12
Source File: FlowableRange.java    From RxJava3-preview with Apache License 2.0 5 votes vote down vote up
@Override
void slowPath(long r) {
    long e = 0;
    int f = end;
    int i = index;
    Subscriber<? super Integer> a = actual;

    for (;;) {

        while (e != r && i != f) {
            if (cancelled) {
                return;
            }

            a.onNext(i);

            e++;
            i++;
        }

        if (i == f) {
            if (!cancelled) {
                a.onComplete();
            }
            return;
        }

        r = get();
        if (e == r) {
            index = i;
            r = addAndGet(-e);
            if (r == 0L) {
                return;
            }
            e = 0L;
        }
    }
}
 
Example 13
Source File: HalfSerializer.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
/**
 * Propagates the given item if possible and terminates if there was a completion or failure event happening during
 * the propagation.
 * The item is drops is the downstream already got a terminal event.
 *
 * @param <T> the type of the item
 * @param subscriber the downstream subscriber
 * @param item the item to propagate downstream
 * @param wip the serialization work-in-progress counter
 * @param container the failure container
 */
public static <T> void onNext(Subscriber<? super T> subscriber, T item,
        AtomicInteger wip, AtomicReference<Throwable> container) {
    if (wip.get() == 0 && wip.compareAndSet(0, 1)) {
        subscriber.onNext(item);
        if (wip.decrementAndGet() != 0) {
            Throwable ex = Subscriptions.terminate(container);
            if (ex != null) {
                subscriber.onError(ex);
            } else {
                subscriber.onComplete();
            }
        }
    }
}
 
Example 14
Source File: Web3jServiceTest.java    From eventeum with Apache License 2.0 4 votes vote down vote up
@Override
protected void subscribeActual(Subscriber<? super T> s) {
    s.onNext(value);
}
 
Example 15
Source File: FlowableFromArray.java    From RxJava3-preview with Apache License 2.0 4 votes vote down vote up
@Override
void slowPath(long r) {
    long e = 0;
    T[] arr = array;
    int f = arr.length;
    int i = index;
    Subscriber<? super T> a = actual;

    for (;;) {

        while (e != r && i != f) {
            if (cancelled) {
                return;
            }

            T t = arr[i];

            if (t == null) {
                a.onError(new NullPointerException("array element is null"));
                return;
            } else {
                a.onNext(t);
            }

            e++;
            i++;
        }

        if (i == f) {
            if (!cancelled) {
                a.onComplete();
            }
            return;
        }

        r = get();
        if (e == r) {
            index = i;
            r = addAndGet(-e);
            if (r == 0L) {
                return;
            }
            e = 0L;
        }
    }
}
 
Example 16
Source File: SmartMulticastProcessor.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 4 votes vote down vote up
void tryDrain() {
	if (done) {
		return;
	}

	int wip;

	if ((wip = WIP.incrementAndGet(this)) > 1) {
		return;
	}

	Subscriber<? super  NewsLetter> a = actual;
	long r = requested;

	for (;;) {
		NewsLetter element = parent.cache;

		if (r > 0 && !sent && element != null) {
			a.onNext(element.withRecipient(getRecipient()));
			sent = true;

			r = REQUESTED.decrementAndGet(this);
		}

		wip = WIP.addAndGet(this, -wip);

		if (wip == 0) {
			if (!done && isTerminated() && hasNoMoreEmission()) {
				done = true;
				if (throwable == null && parent.throwable == null) {
					a.onComplete();
				}
				else {
					throwable = throwable == null ? parent.throwable : throwable;
					a.onError(throwable);
				}
			}

			return;
		}
	}
}
 
Example 17
Source File: MultiGroupByOp.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
void drain() {
    if (wip.getAndIncrement() != 0) {
        return;
    }

    int missed = 1;

    final SpscLinkedArrayQueue<T> q = queue;
    Subscriber<? super T> actual = downstream.get();
    for (;;) {
        if (actual != null) {
            long r = requested.get();
            long e = 0;

            while (e != r) {
                boolean isDone = done.get();
                T v = q.poll();
                boolean empty = v == null;

                if (hasCompleted(isDone, empty, e)) {
                    return;
                }

                if (empty) {
                    break;
                }

                actual.onNext(v);

                e++;
            }

            if (e == r && hasCompleted(done.get(), q.isEmpty(), e)) {
                return;
            }

            if (e != 0L) {
                if (r != Long.MAX_VALUE) {
                    requested.addAndGet(-e);
                }
                parent.upstream.get().request(e);
            }
        }

        missed = wip.addAndGet(-missed);
        if (missed == 0) {
            break;
        }
        if (actual == null) {
            actual = downstream.get();
        }
    }
}
 
Example 18
Source File: UnicastProcessor.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
void drainWithDownstream(Subscriber<? super T> actual) {
    int missed = 1;

    final Queue<T> q = queue;

    for (;;) {

        long r = requested.get();
        long e = 0L;

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

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

            if (isCancelledOrDone(d, empty)) {
                return;
            }

            if (empty) {
                break;
            }

            actual.onNext(t);

            e++;
        }

        if (r == e) {
            if (isCancelledOrDone(done.get(), q.isEmpty())) {
                return;
            }
        }

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

        missed = wip.addAndGet(-missed);
        if (missed == 0) {
            break;
        }
    }
}
 
Example 19
Source File: SerializedSubscriber.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
void serDrainLoop(Subscriber<? super T> actual) {
    for (;;) {

        if (cancelled) {
            return;
        }

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

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

            if (!missed) {
                emitting = false;
                return;
            }

            missed = false;

            d = done;
            e = failure;
            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) {
                    return;
                }

                actual.onNext(arr[i]);
            }

            n = n.next;
        }

        if (cancelled) {
            return;
        }

        if (e != null) {
            actual.onError(e);
            return;
        } else if (d) {
            actual.onComplete();
            return;
        }
    }
}
 
Example 20
Source File: DrainUtils.java    From smallrye-mutiny with Apache License 2.0 4 votes vote down vote up
/**
 * Drains the queue either in a pre- or post-complete state.
 *
 * @param n the requested amount
 * @param downstream the downstream consumer
 * @param queue the queue holding available values
 * @param requested the atomic long keeping track of requests
 * @param isCancelled callback to detect cancellation
 * @return true if the queue was completely drained or the drain process was cancelled
 */
private static <T> boolean postCompleteDrain(long n,
        Subscriber<? super T> downstream,
        Queue<T> queue,
        AtomicLong requested,
        BooleanSupplier isCancelled) {

    long e = n & COMPLETED_MASK;

    for (;;) {

        while (e != n) {
            if (isCancelled.getAsBoolean()) {
                return true;
            }

            T t = queue.poll();

            if (t == null) {
                downstream.onComplete();
                return true;
            }

            downstream.onNext(t);
            e++;
        }

        if (isCancelled.getAsBoolean()) {
            return true;
        }

        if (queue.isEmpty()) {
            downstream.onComplete();
            return true;
        }

        n = requested.get();

        if (n == e) {

            n = requested.addAndGet(-(e & REQUESTED_MASK));

            if ((n & REQUESTED_MASK) == 0L) {
                return false;
            }

            e = n & COMPLETED_MASK;
        }
    }

}