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

The following examples show how to use reactor.core.CoreSubscriber#onComplete() . 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: UnicastProcessor.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
boolean checkTerminated(boolean d, boolean empty, CoreSubscriber<? super T> a, Queue<T> q, @Nullable T t) {
	if (cancelled) {
		Operators.onDiscard(t, a.currentContext());
		Operators.onDiscardQueueWithClear(q, a.currentContext(), null);
		hasDownstream = false;
		return true;
	}
	if (d && empty) {
		Throwable e = error;
		hasDownstream = false;
		if (e != null) {
			a.onError(e);
		} else {
			a.onComplete();
		}
		return true;
	}

	return false;
}
 
Example 2
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 3
Source File: DirectProcessor.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
	Objects.requireNonNull(actual, "subscribe");

	DirectInner<T> p = new DirectInner<>(actual, this);
	actual.onSubscribe(p);

	if (add(p)) {
		if (p.cancelled) {
			remove(p);
		}
	}
	else {
		Throwable e = error;
		if (e != null) {
			actual.onError(e);
		}
		else {
			actual.onComplete();
		}
	}
}
 
Example 4
Source File: FluxPublishMulticast.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
	PublishMulticastInner<T> pcs = new PublishMulticastInner<>(this, actual);
	actual.onSubscribe(pcs);

	if (add(pcs)) {
		if (pcs.requested == Long.MIN_VALUE) {
			remove(pcs);
			return;
		}
		drain();
	}
	else {
		Throwable ex = error;
		if (ex != null) {
			actual.onError(ex);
		}
		else {
			actual.onComplete();
		}
	}
}
 
Example 5
Source File: MonoPublishMulticast.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
	PublishMulticastInner<T> pcs = new PublishMulticastInner<>(this, actual);
	actual.onSubscribe(pcs);

	if (add(pcs)) {
		if (pcs.cancelled == 1) {
			remove(pcs);
			return;
		}
		drain();
	}
	else {
		Throwable ex = error;
		if (ex != null) {
			actual.onError(ex);
		}
		else {
			actual.onComplete();
		}
	}
}
 
Example 6
Source File: FlowableIfNonEmptyComposeTest.java    From akarnokd-misc with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
    TransformSubscription<T> sub = new TransformSubscription<>(actual, this);
    actual.onSubscribe(sub);
    if (transformProducer.compareAndSet(null, sub)) {
        if (sub.cancelled) {
            transformProducer.compareAndSet(sub, null);
            return;
        }
        drain();
    } else {
        if (transformProducer.get() == TERMINATED) {
            Throwable ex = error;
            if (ex == null) {
                actual.onComplete();
            } else {
                actual.onError(ex);
            }
        } else {
            actual.onError(new IllegalStateException("Only one Subscriber allowed at once"));
        }
    }
}
 
Example 7
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 8
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 9
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 10
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 11
Source File: DrainUtils.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * Tries draining the queue if the source just completed.
 *
    * @param <T> the output value type
    * @param <F> the field type holding the requested amount
 * @param actual the consumer of values
 * @param queue the queue holding available values
 * @param field the field updater holding the requested amount
 * @param instance the parent instance of the requested field
 * @param isCancelled callback to detect cancellation
 */
public static <T, F> void postComplete(CoreSubscriber<? super T> actual,
		Queue<T> queue,
		AtomicLongFieldUpdater<F> field,
		F instance,
		BooleanSupplier isCancelled) {

	if (queue.isEmpty()) {
		actual.onComplete();
		return;
	}

	if (postCompleteDrain(field.get(instance), actual, queue, field, instance, isCancelled)) {
		return;
	}

	for (; ; ) {
		long r = field.get(instance);

		if ((r & COMPLETED_MASK) != 0L) {
			return;
		}

		long u = r | COMPLETED_MASK;
		// (active, r) -> (complete, r) transition
		if (field.compareAndSet(instance, r, u)) {
			// if the requested amount was non-zero, drain the queue
			if (r != 0L) {
				postCompleteDrain(u, actual, queue, field, instance, isCancelled);
			}

			return;
		}
	}
}
 
Example 12
Source File: DrainUtils.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * Tries draining the queue if the source just completed.
 *
 * @param <T> the output value type
 * @param <F> the field type holding the requested amount
 * @param actual the consumer of values
 * @param queue the queue of available values
 * @param field the field updater for the requested amount
 * @param instance the parent instance of the requested field 
 * @param isCancelled callback to detect cancellation
 * @param error if not null, the error to signal after the queue has been drained
 */
public static <T, F> void postCompleteDelayError(CoreSubscriber<? super T> actual,
        Queue<T> queue,
        AtomicLongFieldUpdater<F> field,
        F instance,
        BooleanSupplier isCancelled,
  @Nullable Throwable error) {

    if (queue.isEmpty()) {
        if (error == null) {
            actual.onComplete();
        } else {
            actual.onError(error);
        }
        return;
    }

    if (postCompleteDrainDelayError(field.get(instance), actual, queue, field, instance, isCancelled, error)) {
        return;
    }

    for (; ; ) {
        long r = field.get(instance);

        if ((r & COMPLETED_MASK) != 0L) {
            return;
        }

        long u = r | COMPLETED_MASK;
        // (active, r) -> (complete, r) transition
        if (field.compareAndSet(instance, r, u)) {
            // if the requested amount was non-zero, drain the queue
            if (r != 0L) {
                postCompleteDrainDelayError(u, actual, queue, field, instance, isCancelled, error);
            }

            return;
        }
    }
}
 
Example 13
Source File: MonoRunnable.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
    MonoRunnableEagerSubscription s = new MonoRunnableEagerSubscription();
    actual.onSubscribe(s);
    if (s.isCancelled()) {
        return;
    }

    try {
        run.run();
        actual.onComplete();
    } catch (Throwable ex) {
        actual.onError(Operators.onOperatorError(ex, actual.currentContext()));
    }
}
 
Example 14
Source File: FluxReceive.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
final void terminateReceiver(@Nullable Queue<?> q, CoreSubscriber<?> a) {
	if (q != null) {
		q.clear();
	}
	Throwable ex = inboundError;
	receiver = null;
	if (ex != null) {
		//parent.listener.onReceiveError(channel, ex);
		a.onError(ex);
	}
	else {
		a.onComplete();
	}
}
 
Example 15
Source File: FluxEmptySyncFuseable.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
	actual.onSubscribe(new SynchronousSubscription<T>() {
		@Override
		@Nullable
		public T poll() {
			return null;
		}

		@Override
		public int size() {
			return 0;
		}

		@Override
		public boolean isEmpty() {
			return true;
		}

		@Override
		public void clear() {

		}

		@Override
		public void request(long n) {

		}

		@Override
		public void cancel() {

		}
	});
	actual.onComplete();
}
 
Example 16
Source File: FluxReceive.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
final void onInboundComplete() {
	if (inboundDone) {
		return;
	}
	inboundDone = true;
	if (receiverFastpath) {
		CoreSubscriber<?> receiver = this.receiver;
		if (receiver != null) {
			receiver.onComplete();
		}
		return;
	}
	drainReceiver();
}
 
Example 17
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
boolean drain() {
    if (WIP.getAndIncrement(this) != 0) {
        return false;
    }

    T f = this.first;
    int m = 1;
    boolean sent = false;
    CoreSubscriber<? super T> a;

    for (;;) {
        a = this.inner;

        // check for the case where upstream terminates before downstream has subscribed at all
        if (a != null) {
            if (f != null && this.requestedOnce) {
                this.first = null;

                if (a == Operators.EMPTY_SUBSCRIBER) {
                    Operators.onDiscard(f, currentContext());
                    return false;
                }

                sent = tryOnNext(a, f);
                f = null;
                // check if not cancelled if it has just been sent (next + cancel case)
                a = this.inner;
            }

            if (a == Operators.EMPTY_SUBSCRIBER) {
                return false;
            }

            if (this.done && f == null) {
                Throwable t = this.throwable;
                if (t != null) {
                    a.onError(t);
                } else {
                    a.onComplete();
                }
                INNER.lazySet(this, Operators.EMPTY_SUBSCRIBER);
                return sent;
            }
        }

        m = WIP.addAndGet(this, -m);
        if (m == 0) {
            return sent;
        }
    }
}
 
Example 18
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;
		}
	}
}
 
Example 19
Source File: HttpClientOperations.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
void _subscribe(CoreSubscriber<? super Void> s) {
	HttpDataFactory df = DEFAULT_FACTORY;

	try {
		HttpClientFormEncoder encoder = new HttpClientFormEncoder(df,
				parent.nettyRequest,
				false,
				HttpConstants.DEFAULT_CHARSET,
				HttpPostRequestEncoder.EncoderMode.RFC1738);

		formCallback.accept(parent, encoder);

		encoder = encoder.applyChanges(parent.nettyRequest);
		df = encoder.newFactory;

		if (!encoder.isMultipart()) {
			parent.requestHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
		}

		// Returned value is deliberately ignored
		parent.addHandlerFirst(NettyPipeline.ChunkedWriter, new ChunkedWriteHandler());

		boolean chunked = HttpUtil.isTransferEncodingChunked(parent.nettyRequest);

		HttpRequest r = encoder.finalizeRequest();

		if (!chunked) {
			HttpUtil.setTransferEncodingChunked(r, false);
			HttpUtil.setContentLength(r, encoder.length());
		}

		ChannelFuture f = parent.channel()
		                        .writeAndFlush(r);

		Flux<Long> tail = encoder.progressFlux.onBackpressureLatest();

		if (encoder.cleanOnTerminate) {
			tail = tail.doOnCancel(encoder)
			           .doAfterTerminate(encoder);
		}

		if (encoder.isChunked()) {
			if (progressCallback != null) {
				progressCallback.accept(tail);
			}
			//"FutureReturnValueIgnored" this is deliberate
			parent.channel()
			      .writeAndFlush(encoder);
		}
		else {
			if (progressCallback != null) {
				progressCallback.accept(FutureMono.from(f)
				                                  .cast(Long.class)
				                                  .switchIfEmpty(Mono.just(encoder.length()))
				                                  .flux());
			}
		}
		s.onComplete();


	}
	catch (Throwable e) {
		Exceptions.throwIfJvmFatal(e);
		df.cleanRequestHttpData(parent.nettyRequest);
		s.onError(Exceptions.unwrap(e));
	}
}