Interface Stream.Listener
- All Known Implementing Classes:
 HTTP2ServerConnectionFactory.HTTPServerSessionListener
- Enclosing interface:
 Stream
A Stream.Listener is the passive counterpart of a Stream and receives
events happening on an HTTP/2 stream.
HTTP/2 data is flow controlled - this means that only a finite number of data events are delivered, until the flow control window is exhausted.
Applications control the delivery of data events by requesting them via
Stream.demand(); the first event is always delivered, while subsequent
events must be explicitly demanded.
Applications control the HTTP/2 flow control by completing the callback associated with data events - this allows the implementation to recycle the data buffer and eventually to enlarge the flow control window so that the sender can send more data.
- See Also:
 
- 
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stream.ListenerA convenient constant for aStream.Listenerimplementation that demands and discards DATA frames, typically to be returned fromSession.Listener.onNewStream(Stream, HeadersFrame)andonPush(Stream, PushPromiseFrame). - 
Method Summary
Modifier and TypeMethodDescriptiondefault voidCallback method invoked after the stream has been closed.default voidonDataAvailable(Stream stream) A simplified version ofonDataAvailable(Stream, boolean).default voidonDataAvailable(Stream stream, boolean immediate) Callback method invoked if the application has expresseddemandfor DATA frames, and if there is content available.default voidCallback method invoked when the stream failed.default voidonHeaders(Stream stream, HeadersFrame frame) Callback method invoked when a HEADERS frame representing the HTTP response has been received.default voidonHeaders(Stream stream, HeadersFrame frame, Callback callback) Callback method invoked when a HEADERS frame representing the HTTP response has been received.default voidonIdleTimeout(Stream stream, TimeoutException x, Promise<Boolean> promise) Callback method invoked when the stream exceeds its idle timeout.default voidonNewStream(Stream stream) Callback method invoked when a stream is created locally bySession.newStream(HeadersFrame, Promise, Listener).default Stream.ListeneronPush(Stream stream, PushPromiseFrame frame) Callback method invoked when a PUSH_PROMISE frame has been received.default voidonReset(Stream stream, ResetFrame frame, Callback callback) Callback method invoked when a RST_STREAM frame has been received for this stream. 
- 
Field Details
- 
AUTO_DISCARD
A convenient constant for a
Stream.Listenerimplementation that demands and discards DATA frames, typically to be returned fromSession.Listener.onNewStream(Stream, HeadersFrame)andonPush(Stream, PushPromiseFrame). 
 - 
 - 
Method Details
- 
onNewStream
Callback method invoked when a stream is created locally by
Session.newStream(HeadersFrame, Promise, Listener).- Parameters:
 stream- the newly created stream
 - 
onHeaders
Callback method invoked when a HEADERS frame representing the HTTP response has been received.
This overload version should be used for simple synchronous implementations.
- Parameters:
 stream- the streamframe- the HEADERS frame received
 - 
onHeaders
Callback method invoked when a HEADERS frame representing the HTTP response has been received.
This overload version allows for asynchronous implementations.
- Parameters:
 stream- the streamframe- the HEADERS frame receivedcallback- the callback to notify when the processing is complete
 - 
onPush
Callback method invoked when a PUSH_PROMISE frame has been received.
Applications that override this method are typically interested in processing the pushed stream DATA frames, and must demand for pushed DATA frames via
Stream.demand()and then return aStream.Listenerimplementation that overrides eitheronDataAvailable(Stream)oronDataAvailable(Stream, boolean), where applications can read from theStreamviaStream.readData(), or returnAUTO_DISCARDthat automatically reads and discards DATA frames. Returningnullis possible but discouraged, and has the same effect of demanding and discarding the pushed DATA frames.- Parameters:
 stream- the pushed streamframe- the PUSH_PROMISE frame received- Returns:
 - a Stream.Listener that will be notified of pushed stream events
 
 - 
onDataAvailable
A simplified version of
onDataAvailable(Stream, boolean).The default implementation of this method reads and discards data.
- Parameters:
 stream- the stream- See Also:
 
 - 
onDataAvailable
Callback method invoked if the application has expressed
demandfor DATA frames, and if there is content available.Applications that wish to handle DATA frames should call
Stream.demand()for this method to be invoked when the data is available.Server applications should typically demand from
onNewStream(Stream)(upon receiving an HTTP request), while client applications should typically demand fromonHeaders(Stream, HeadersFrame)(upon receiving an HTTP response).Just prior calling this method, the outstanding demand is cancelled; applications that implement this method should read content calling
Stream.readData(), and callStream.demand()to signal to the implementation to call again this method when there may be more content available.Only one thread at a time invokes this method, although it may not be the same thread across different invocations.
It is always guaranteed that invoking
Stream.demand()from within this method will not cause aStackOverflowError.Typical usage:
class MyStreamListener implements Stream.Listener { @Override public void onDataAvailable(Stream stream, boolean immediate) { // Read a chunk of the content. Stream.Data data = stream.readData(); if (data == null) { // No data available now, demand to be called back. stream.demand(); } else { // Process the content. process(data.frame().getByteBuffer()); // Notify that the content has been consumed. data.release(); if (!data.frame().isEndStream()) { // Demand to be called back. stream.demand(); } } } }The default implementation of this method calls
onDataAvailable(Stream).- Parameters:
 stream- the streamimmediate-truewhen data is immediately available at the timeStream.demand()is invoked (this method is directly invoked fromStream.demand();falsewhen data was not immediately available at the timeStream.demand()was called, but is now available (this method is invoked from the network layer, not directly fromStream.demand()- See Also:
 
 - 
onReset
Callback method invoked when a RST_STREAM frame has been received for this stream.
- Parameters:
 stream- the streamframe- the RST_STREAM frame receivedcallback- the callback to complete when the reset has been handled
 - 
onIdleTimeout
Callback method invoked when the stream exceeds its idle timeout.
- Parameters:
 stream- the streamx- the timeout failurepromise- the promise to complete- See Also:
 
 - 
onFailure
default void onFailure(Stream stream, int error, String reason, Throwable failure, Callback callback) Callback method invoked when the stream failed.
- Parameters:
 stream- the streamerror- the error codereason- the error reason, or nullfailure- the failurecallback- the callback to complete when the failure has been handled
 - 
onClosed
Callback method invoked after the stream has been closed.
- Parameters:
 stream- the stream
 
 -