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 class
AsyncSubscriber.OnComplete
private static class
AsyncSubscriber.OnError
private static class
AsyncSubscriber.OnNext<T>
private static class
AsyncSubscriber.OnSubscribe
private static interface
AsyncSubscriber.Signal
-
Field Summary
Fields Modifier and Type Field Description private boolean
done
private java.util.concurrent.Executor
executor
private java.util.concurrent.ConcurrentLinkedQueue<AsyncSubscriber.Signal>
inboundSignals
private java.util.concurrent.atomic.AtomicBoolean
on
private Subscription
subscription
-
Constructor Summary
Constructors Modifier Constructor Description protected
AsyncSubscriber(java.util.concurrent.Executor executor)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private void
done()
private void
handleOnComplete()
private void
handleOnError(java.lang.Throwable error)
private void
handleOnNext(T element)
private void
handleOnSubscribe(Subscription s)
void
onComplete()
Successful terminal state.void
onError(java.lang.Throwable t)
Failed terminal state.void
onNext(T element)
Data notification sent by thePublisher
in response to requests toSubscription.request(long)
.void
onSubscribe(Subscription s)
Invoked after callingPublisher.subscribe(Subscriber)
.void
run()
private void
signal(AsyncSubscriber.Signal signal)
private void
tryScheduleToExecute()
protected void
whenComplete()
protected void
whenError(java.lang.Throwable error)
protected abstract boolean
whenNext(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:Subscriber
Invoked after callingPublisher.subscribe(Subscriber)
.No data will start flowing until
Subscription.request(long)
is invoked.It is the responsibility of this
Subscriber
instance to callSubscription.request(long)
whenever more data is wanted.The
Publisher
will send notifications only in response toSubscription.request(long)
.- Specified by:
onSubscribe
in interfaceSubscriber<T>
- Parameters:
s
- theSubscription
that allows requesting data viaSubscription.request(long)
-
onNext
public final void onNext(T element)
Description copied from interface:Subscriber
Data notification sent by thePublisher
in response to requests toSubscription.request(long)
.- Specified by:
onNext
in interfaceSubscriber<T>
- Parameters:
element
- the element signaled
-
onError
public final void onError(java.lang.Throwable t)
Description copied from interface:Subscriber
Failed terminal state.No further events will be sent even if
Subscription.request(long)
is invoked again.- Specified by:
onError
in interfaceSubscriber<T>
- Parameters:
t
- the throwable signaled
-
onComplete
public final void onComplete()
Description copied from interface:Subscriber
Successful terminal state.No further events will be sent even if
Subscription.request(long)
is invoked again.- Specified by:
onComplete
in interfaceSubscriber<T>
-
run
public final void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
signal
private void signal(AsyncSubscriber.Signal signal)
-
tryScheduleToExecute
private final void tryScheduleToExecute()
-
-