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
Modifier and TypeFieldDescriptionstatic final Stream.Listener
A convenient constant for aStream.Listener
implementation that demands and discards DATA frames, typically to be returned fromSession.Listener.onNewStream(Stream, HeadersFrame)
andonPush(Stream, PushPromiseFrame)
. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Callback method invoked after the stream has been closed.default void
onDataAvailable
(Stream stream) Callback method invoked if the application has expresseddemand
for DATA frames, and if there may be content available.default void
Callback method invoked when the stream failed.default void
onHeaders
(Stream stream, HeadersFrame frame) Callback method invoked when a HEADERS frame representing the HTTP response has been received.default void
onIdleTimeout
(Stream stream, TimeoutException x, Promise<Boolean> promise) Callback method invoked when the stream exceeds its idle timeout.default void
onNewStream
(Stream stream) Callback method invoked when a stream is created locally bySession.newStream(HeadersFrame, Promise, Listener)
.default Stream.Listener
onPush
(Stream stream, PushPromiseFrame frame) Callback method invoked when a PUSH_PROMISE frame has been received.default void
onReset
(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.Listener
implementation 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.
- Parameters:
stream
- the streamframe
- the HEADERS frame received
-
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 either aStream.Listener
implementation that overridesonDataAvailable(Stream)
where applications can read from theStream
viaStream.readData()
, orAUTO_DISCARD
that automatically reads and discards DATA frames. Returningnull
is 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
Callback method invoked if the application has expressed
demand
for DATA frames, and if there may be 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) { // 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(); } } } }
- Parameters:
stream
- the stream- 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
-