public abstract class AbstractListenerWriteProcessor<T>
extends java.lang.Object
Processor
implementations that bridge between
event-listener write APIs and Reactive Streams.
Specifically a base class for writing to the HTTP response body with Servlet 3.1 non-blocking I/O and Undertow XNIO as well for writing WebSocket messages through the Java WebSocket API (JSR-356), Jetty, and Undertow.
Modifier and Type | Class and Description |
---|---|
private static class |
AbstractListenerWriteProcessor.State
Represents a state for the
Processor to be in. |
Modifier and Type | Field and Description |
---|---|
private T |
currentData |
protected Log |
logger |
private WriteResultPublisher |
resultPublisher |
private java.util.concurrent.atomic.AtomicReference<AbstractListenerWriteProcessor.State> |
state |
private boolean |
subscriberCompleted |
private Subscription |
subscription |
Constructor and Description |
---|
AbstractListenerWriteProcessor() |
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Invoked during an error or completion callback from the underlying
container to cancel the upstream subscription.
|
private boolean |
changeState(AbstractListenerWriteProcessor.State oldState,
AbstractListenerWriteProcessor.State newState) |
private void |
changeStateToComplete(AbstractListenerWriteProcessor.State oldState) |
protected void |
dataReceived(T data)
Template method invoked after a data item to write is received via
Subscriber#onNext(Object) . |
protected abstract boolean |
isDataEmpty(T data)
Whether the given data item has any content to write.
|
protected abstract boolean |
isWritePossible()
Whether writing is possible.
|
void |
onComplete()
Completion signal from the upstream, write Publisher.
|
void |
onError(java.lang.Throwable ex)
Error signal from the upstream, write Publisher.
|
void |
onNext(T data) |
void |
onSubscribe(Subscription subscription) |
void |
onWritePossible()
Invoked when writing is possible, either in the same thread after a check
via
isWritePossible() , or as a callback from the underlying
container. |
void |
subscribe(<any> subscriber) |
protected abstract boolean |
write(T data)
Write the given item.
|
private void |
writeIfPossible() |
protected void |
writingComplete()
Invoked after onComplete or onError notification.
|
protected void |
writingFailed(java.lang.Throwable ex)
Invoked when an I/O error occurs during a write.
|
protected void |
writingPaused()
Invoked after the current data has been written and before requesting
the next item from the upstream, write Publisher.
|
protected final Log logger
private final java.util.concurrent.atomic.AtomicReference<AbstractListenerWriteProcessor.State> state
@Nullable private Subscription subscription
private volatile boolean subscriberCompleted
private final WriteResultPublisher resultPublisher
public final void onSubscribe(Subscription subscription)
public final void onNext(T data)
public final void onError(java.lang.Throwable ex)
public final void onComplete()
public final void onWritePossible()
isWritePossible()
, or as a callback from the underlying
container.public void cancel()
public final void subscribe(<any> subscriber)
protected abstract boolean isDataEmpty(T data)
protected void dataReceived(T data)
Subscriber#onNext(Object)
. The default implementation saves the
data item for writing once that is possible.protected abstract boolean isWritePossible()
protected abstract boolean write(T data) throws java.io.IOException
Note: Sub-classes are responsible for releasing any data buffer associated with the item, once fully written, if pooled buffers apply to the underlying container.
data
- the item to writetrue
), or or otherwise if more writes are required.java.io.IOException
protected void writingPaused()
The default implementation is a no-op.
protected void writingComplete()
The default implementation is a no-op.
protected void writingFailed(java.lang.Throwable ex)
Defaults to no-op.
private boolean changeState(AbstractListenerWriteProcessor.State oldState, AbstractListenerWriteProcessor.State newState)
private void changeStateToComplete(AbstractListenerWriteProcessor.State oldState)
private void writeIfPossible()