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

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

    for (int i = index; i != f; i++) {
        if (cancelled) {
            return;
        }
        T t = arr[i];
        if (t == null) {
            a.onError(new NullPointerException("array element is null"));
            return;
        } else {
            a.onNext(t);
        }
    }
    if (cancelled) {
        return;
    }
    a.onComplete();
}
 
Example 2
Source File: FileAsyncRequestBody.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribe(Subscriber<? super ByteBuffer> s) {
    try {
        AsynchronousFileChannel channel = openInputChannel(this.path);

        // We need to synchronize here because the subscriber could call
        // request() from within onSubscribe which would potentially
        // trigger onNext before onSubscribe is finished.
        Subscription subscription = new FileSubscription(channel, s, chunkSizeInBytes);
        synchronized (subscription) {
            s.onSubscribe(subscription);
        }
    } catch (IOException e) {
        // subscribe() must return normally, so we need to signal the
        // failure to open via onError() once onSubscribe() is signaled.
        s.onSubscribe(new NoopSubscription(s));
        s.onError(e);
    }
}
 
Example 3
Source File: FlowableFromCallable.java    From RxJava3-preview with Apache License 2.0 6 votes vote down vote up
@Override
public void subscribeActual(Subscriber<? super T> s) {
    DeferredScalarSubscription<T> deferred = new DeferredScalarSubscription<T>(s);
    s.onSubscribe(deferred);

    T t;
    try {
        t = ObjectHelper.requireNonNull(callable.call(), "The callable returned a null value");
    } catch (Throwable ex) {
        Exceptions.throwIfFatal(ex);
        s.onError(ex);
        return;
    }

    deferred.complete(t);
}
 
Example 4
Source File: AbstractStreamObserverAndPublisher.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean checkTerminated(boolean d, boolean empty, Subscriber<? super T> subscriber, Queue<T> q) {
    if (cancelled) {
        q.clear();
        downstream = null;
        return true;
    }

    if (d && empty) {
        Throwable e = error;
        downstream = null;
        if (e != null) {
            subscriber.onError(e);
        } else {
            subscriber.onComplete();
        }
        return true;
    }

    return false;
}
 
Example 5
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 6
Source File: MultiWindowOp.java    From smallrye-mutiny with Apache License 2.0 6 votes vote down vote up
boolean isCancelledOrDone(boolean isDone, boolean isEmpty, Subscriber<?> subscriber, Queue<?> q) {
    if (isCancelled()) {
        q.clear();
        return true;
    }

    if (isDone) {
        Throwable failed = failure.get();

        if (failed != null) {
            q.clear();
            subscriber.onError(failed);
            return true;
        } else if (isEmpty) {
            subscriber.onComplete();
            return true;
        }
    }

    return false;
}
 
Example 7
Source File: KafkaSink.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable throwable) {
    Subscriber<? super Message<?>> subscriber = downstream.getAndSet(null);
    if (subscriber != null) {
        subscriber.onError(throwable);
    }
}
 
Example 8
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 9
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 10
Source File: AbstractListenerReadPublisher.java    From java-technology-stack with MIT License 5 votes vote down vote up
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable t) {
	if (publisher.changeState(this, COMPLETED)) {
		publisher.discardData();
		Subscriber<? super T> s = publisher.subscriber;
		if (s != null) {
			s.onError(t);
		}
	}
	else {
		publisher.state.get().onError(publisher, t);
	}
}
 
Example 11
Source File: WriteResultPublisher.java    From spring-analysis-note with MIT License 5 votes vote down vote up
void publishError(WriteResultPublisher publisher, Throwable t) {
	if (publisher.changeState(this, COMPLETED)) {
		Subscriber<? super Void> s = publisher.subscriber;
		Assert.state(s != null, "No subscriber");
		s.onError(t);
	}
	else {
		publisher.state.get().publishError(publisher, t);
	}
}
 
Example 12
Source File: AbstractListenerReadPublisher.java    From spring-analysis-note with MIT License 5 votes vote down vote up
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable t) {
	if (publisher.changeState(this, COMPLETED)) {
		publisher.discardData();
		Subscriber<? super T> s = publisher.subscriber;
		if (s != null) {
			s.onError(t);
		}
	}
	else {
		publisher.state.get().onError(publisher, t);
	}
}
 
Example 13
Source File: AbstractStreamObserverAndPublisher.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void drainFused(final Subscriber<? super T> subscriber) {
    int missed = 1;

    for (;;) {
        if (cancelled) {
            queue.clear();
            downstream = null;
            return;
        }

        boolean d = done;

        subscriber.onNext(null);

        if (d) {
            downstream = null;

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

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

        if (missed == 0) {
            break;
        }
    }
}
 
Example 14
Source File: HandlerPublisher.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void provideSubscriber(Subscriber<? super T> subscriber) {
    this.subscriber = subscriber;
    switch (state) {
        case NO_SUBSCRIBER_OR_CONTEXT:
            state = HandlerPublisher.State.NO_CONTEXT;
            break;
        case NO_SUBSCRIBER:
            if (buffer.isEmpty()) {
                state = HandlerPublisher.State.IDLE;
            } else {
                state = HandlerPublisher.State.BUFFERING;
            }
            subscriber.onSubscribe(new ChannelSubscription());
            break;
        case DRAINING:
            subscriber.onSubscribe(new ChannelSubscription());
            break;
        case NO_SUBSCRIBER_ERROR:
            cleanup();
            state = HandlerPublisher.State.DONE;
            subscriber.onSubscribe(new ChannelSubscription());
            subscriber.onError(noSubscriberError);
            break;
        default:
            // Do nothing
    }
}
 
Example 15
Source File: Subscriptions.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
public static void terminateAndPropagate(AtomicReference<Throwable> failures, Subscriber<?> subscriber) {
    Throwable ex = markFailureAsTerminated(failures);
    if (ex == null) {
        subscriber.onComplete();
    } else if (ex != TERMINATED) {
        subscriber.onError(ex);
    }
}
 
Example 16
Source File: Subscriptions.java    From smallrye-mutiny with Apache License 2.0 5 votes vote down vote up
public static void fail(Subscriber<?> subscriber, Throwable failure, Publisher<?> upstream) {
    ParameterValidation.nonNull(subscriber, "subscriber");
    ParameterValidation.nonNull(failure, "failure");
    if (upstream != null) {
        upstream.subscribe(new CancelledSubscriber<>());
    }

    subscriber.onSubscribe(empty());
    subscriber.onError(failure);
}
 
Example 17
Source File: ConnectableProcessor.java    From smallrye-reactive-streams-operators with Apache License 2.0 4 votes vote down vote up
private void manageSubscribeInFailedState(Subscriber<? super T> subscriber) {
    subscriber.onSubscribe(new EmptySubscription());
    subscriber.onError(failure.get());
}
 
Example 18
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 19
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 20
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;
		}
	}
}