T
- the type of element signaled to the Subscriber
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 |
private java.lang.String |
logPrefix |
private WriteResultPublisher |
resultPublisher |
protected static Log |
rsWriteLogger
Special logger for debugging Reactive Streams signals.
|
private java.util.concurrent.atomic.AtomicReference<AbstractListenerWriteProcessor.State> |
state |
private boolean |
subscriberCompleted |
private Subscription |
subscription |
Constructor and Description |
---|
AbstractListenerWriteProcessor() |
AbstractListenerWriteProcessor(java.lang.String logPrefix)
Create an instance with the given log prefix.
|
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) |
private void |
changeStateToReceived(AbstractListenerWriteProcessor.State oldState) |
protected void |
dataReceived(T data)
Template method invoked after a data item to write is received via
Subscriber#onNext(Object) . |
private void |
discardCurrentData() |
protected abstract void |
discardData(T data)
Invoked after any error (either from the upstream write Publisher, or
from I/O operations to the underlying server) and cancellation
to discard in-flight data that was in
the process of being written when the error took place.
|
java.lang.String |
getLogPrefix()
Create an instance with the given log prefix.
|
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()
Deprecated.
originally introduced for Undertow to stop write notifications
when no data is available, but deprecated as of as of 5.0.6 since constant
switching on every requested item causes a significant slowdown.
|
protected static final Log rsWriteLogger
private final java.util.concurrent.atomic.AtomicReference<AbstractListenerWriteProcessor.State> state
@Nullable private Subscription subscription
private volatile boolean subscriberCompleted
private final WriteResultPublisher resultPublisher
private final java.lang.String logPrefix
public AbstractListenerWriteProcessor()
public AbstractListenerWriteProcessor(java.lang.String logPrefix)
public java.lang.String getLogPrefix()
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
@Deprecated 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.
protected abstract void discardData(T data)
data
- the data to be releasedprivate boolean changeState(AbstractListenerWriteProcessor.State oldState, AbstractListenerWriteProcessor.State newState)
private void changeStateToReceived(AbstractListenerWriteProcessor.State oldState)
private void changeStateToComplete(AbstractListenerWriteProcessor.State oldState)
private void writeIfPossible()
private void discardCurrentData()