Class AsyncSubscriber<T>
- java.lang.Object
-
- org.reactivestreams.example.unicast.AsyncSubscriber<T>
-
- All Implemented Interfaces:
java.lang.Runnable,Subscriber<T>
public abstract class AsyncSubscriber<T> extends java.lang.Object implements Subscriber<T>, java.lang.Runnable
AsyncSubscriber is an implementation of Reactive Streams `Subscriber`, it runs asynchronously (on an Executor), requests one element at a time, and invokes a user-defined method to process each element. NOTE: The code below uses a lot of try-catches to show the reader where exceptions can be expected, and where they are forbidden.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classAsyncSubscriber.OnCompleteprivate static classAsyncSubscriber.OnErrorprivate static classAsyncSubscriber.OnNext<T>private static classAsyncSubscriber.OnSubscribeprivate static interfaceAsyncSubscriber.Signal
-
Field Summary
Fields Modifier and Type Field Description private booleandoneprivate java.util.concurrent.Executorexecutorprivate java.util.concurrent.ConcurrentLinkedQueue<AsyncSubscriber.Signal>inboundSignalsprivate java.util.concurrent.atomic.AtomicBooleanonprivate Subscriptionsubscription
-
Constructor Summary
Constructors Modifier Constructor Description protectedAsyncSubscriber(java.util.concurrent.Executor executor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private voiddone()private voidhandleOnComplete()private voidhandleOnError(java.lang.Throwable error)private voidhandleOnNext(T element)private voidhandleOnSubscribe(Subscription s)voidonComplete()Successful terminal state.voidonError(java.lang.Throwable t)Failed terminal state.voidonNext(T element)Data notification sent by thePublisherin response to requests toSubscription.request(long).voidonSubscribe(Subscription s)Invoked after callingPublisher.subscribe(Subscriber).voidrun()private voidsignal(AsyncSubscriber.Signal signal)private voidtryScheduleToExecute()protected voidwhenComplete()protected voidwhenError(java.lang.Throwable error)protected abstract booleanwhenNext(T element)
-
-
-
Field Detail
-
subscription
private Subscription subscription
-
done
private boolean done
-
executor
private final java.util.concurrent.Executor executor
-
inboundSignals
private final java.util.concurrent.ConcurrentLinkedQueue<AsyncSubscriber.Signal> inboundSignals
-
on
private final java.util.concurrent.atomic.AtomicBoolean on
-
-
Method Detail
-
done
private final void done()
-
whenNext
protected abstract boolean whenNext(T element)
-
whenComplete
protected void whenComplete()
-
whenError
protected void whenError(java.lang.Throwable error)
-
handleOnSubscribe
private final void handleOnSubscribe(Subscription s)
-
handleOnNext
private final void handleOnNext(T element)
-
handleOnComplete
private void handleOnComplete()
-
handleOnError
private void handleOnError(java.lang.Throwable error)
-
onSubscribe
public final void onSubscribe(Subscription s)
Description copied from interface:SubscriberInvoked after callingPublisher.subscribe(Subscriber).No data will start flowing until
Subscription.request(long)is invoked.It is the responsibility of this
Subscriberinstance to callSubscription.request(long)whenever more data is wanted.The
Publisherwill send notifications only in response toSubscription.request(long).- Specified by:
onSubscribein interfaceSubscriber<T>- Parameters:
s- theSubscriptionthat allows requesting data viaSubscription.request(long)
-
onNext
public final void onNext(T element)
Description copied from interface:SubscriberData notification sent by thePublisherin response to requests toSubscription.request(long).- Specified by:
onNextin interfaceSubscriber<T>- Parameters:
element- the element signaled
-
onError
public final void onError(java.lang.Throwable t)
Description copied from interface:SubscriberFailed terminal state.No further events will be sent even if
Subscription.request(long)is invoked again.- Specified by:
onErrorin interfaceSubscriber<T>- Parameters:
t- the throwable signaled
-
onComplete
public final void onComplete()
Description copied from interface:SubscriberSuccessful terminal state.No further events will be sent even if
Subscription.request(long)is invoked again.- Specified by:
onCompletein interfaceSubscriber<T>
-
run
public final void run()
- Specified by:
runin interfacejava.lang.Runnable
-
signal
private void signal(AsyncSubscriber.Signal signal)
-
tryScheduleToExecute
private final void tryScheduleToExecute()
-
-