Package org.asynchttpclient
Interface AsyncHandler<T>
-
- Type Parameters:
T- Type of object returned by theFuture.get()
- All Known Subinterfaces:
ProgressAsyncHandler<T>
- All Known Implementing Classes:
AsyncCompletionHandler,AsyncCompletionHandlerBase,BodyDeferringAsyncHandler,ResumableAsyncHandler,TransferCompletionHandler,WebSocketUpgradeHandler
public interface AsyncHandler<T>An asynchronous handler or callback which gets invoked as soon as some data is available when processing an asynchronous response.
Callback methods get invoked in the following order:onStatusReceived(HttpResponseStatus),onHeadersReceived(HttpHeaders),onBodyPartReceived(HttpResponseBodyPart), which could be invoked multiple times,onTrailingHeadersReceived(HttpHeaders), which is only invoked if trailing HTTP headers are receivedonCompleted(), once the response has been fully read.
Returning aAsyncHandler.State.ABORTfrom any of those callback methods will interrupt asynchronous response processing. After that, onlyonCompleted()is going to be called.
AsyncHandlers aren't thread safe. Hence, you should avoid re-using the same instance when doing concurrent requests. As an example, the following may produce unexpected results:
It is recommended to create a new instance instead.AsyncHandler ah = new AsyncHandler() {....}; AsyncHttpClient client = new AsyncHttpClient(); client.prepareGet("http://...").execute(ah); client.prepareGet("http://...").execute(ah);Do NOT perform any blocking operations in any of these methods. A typical example would be trying to send another request and calling get() on its future. There's a chance you might end up in a deadlock. If you really need to perform a blocking operation, execute it in a different dedicated thread pool.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classAsyncHandler.State
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description AsyncHandler.StateonBodyPartReceived(HttpResponseBodyPart bodyPart)Invoked as soon as some response body part are received.TonCompleted()Invoked once the HTTP response processing is finished.default voidonConnectionOffer(io.netty.channel.Channel connection)Notify the callback when trying to offer a connection to the pool.default voidonConnectionPoolAttempt()Notify the callback when trying to fetch a connection from the pool.default voidonConnectionPooled(io.netty.channel.Channel connection)Notify the callback when a new connection was successfully fetched from the pool.AsyncHandler.StateonHeadersReceived(io.netty.handler.codec.http.HttpHeaders headers)Invoked as soon as the HTTP headers have been received.default voidonHostnameResolutionAttempt(String name)Notify the callback before hostname resolutiondefault voidonHostnameResolutionFailure(String name, Throwable cause)Notify the callback after hostname resolution failed.default voidonHostnameResolutionSuccess(String name, List<InetSocketAddress> addresses)Notify the callback after hostname resolution was successful.default voidonRequestSend(NettyRequest request)Notify the callback when a request is being written on the channel.default voidonRetry()Notify the callback every time a request is being retried.AsyncHandler.StateonStatusReceived(HttpResponseStatus responseStatus)Invoked as soon as the HTTP status line has been receiveddefault voidonTcpConnectAttempt(InetSocketAddress remoteAddress)Notify the callback when trying to open a new connection.default voidonTcpConnectFailure(InetSocketAddress remoteAddress, Throwable cause)Notify the callback after a failed connect.default voidonTcpConnectSuccess(InetSocketAddress remoteAddress, io.netty.channel.Channel connection)Notify the callback after a successful connectvoidonThrowable(Throwable t)Invoked when an unexpected exception occurs during the processing of the response.default voidonTlsHandshakeAttempt()Notify the callback before TLS handshakedefault voidonTlsHandshakeFailure(Throwable cause)Notify the callback after the TLS faileddefault voidonTlsHandshakeSuccess(SSLSession sslSession)Notify the callback after the TLS was successfuldefault AsyncHandler.StateonTrailingHeadersReceived(io.netty.handler.codec.http.HttpHeaders headers)Invoked when trailing headers have been received.
-
-
-
Method Detail
-
onStatusReceived
AsyncHandler.State onStatusReceived(HttpResponseStatus responseStatus) throws Exception
Invoked as soon as the HTTP status line has been received- Parameters:
responseStatus- the status code and test of the response- Returns:
- a
AsyncHandler.Statetelling to CONTINUE or ABORT the current processing. - Throws:
Exception- if something wrong happens
-
onHeadersReceived
AsyncHandler.State onHeadersReceived(io.netty.handler.codec.http.HttpHeaders headers) throws Exception
Invoked as soon as the HTTP headers have been received.- Parameters:
headers- the HTTP headers.- Returns:
- a
AsyncHandler.Statetelling to CONTINUE or ABORT the current processing. - Throws:
Exception- if something wrong happens
-
onBodyPartReceived
AsyncHandler.State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception
Invoked as soon as some response body part are received. Could be invoked many times. Beware that, depending on the provider (Netty) this can be notified with empty body parts.- Parameters:
bodyPart- response's body part.- Returns:
- a
AsyncHandler.Statetelling to CONTINUE or ABORT the current processing. Aborting will also close the connection. - Throws:
Exception- if something wrong happens
-
onTrailingHeadersReceived
default AsyncHandler.State onTrailingHeadersReceived(io.netty.handler.codec.http.HttpHeaders headers) throws Exception
Invoked when trailing headers have been received.- Parameters:
headers- the trailing HTTP headers.- Returns:
- a
AsyncHandler.Statetelling to CONTINUE or ABORT the current processing. - Throws:
Exception- if something wrong happens
-
onThrowable
void onThrowable(Throwable t)
Invoked when an unexpected exception occurs during the processing of the response. The exception may have been produced by implementation of onXXXReceived method invocation.- Parameters:
t- aThrowable
-
onCompleted
@Nullable T onCompleted() throws Exception
Invoked once the HTTP response processing is finished.
Gets always invoked as last callback method.
-
onHostnameResolutionAttempt
default void onHostnameResolutionAttempt(String name)
Notify the callback before hostname resolution- Parameters:
name- the name to be resolved
-
onHostnameResolutionSuccess
default void onHostnameResolutionSuccess(String name, List<InetSocketAddress> addresses)
Notify the callback after hostname resolution was successful.- Parameters:
name- the name to be resolvedaddresses- the resolved addresses
-
onHostnameResolutionFailure
default void onHostnameResolutionFailure(String name, Throwable cause)
Notify the callback after hostname resolution failed.- Parameters:
name- the name to be resolvedcause- the failure cause
-
onTcpConnectAttempt
default void onTcpConnectAttempt(InetSocketAddress remoteAddress)
Notify the callback when trying to open a new connection.Might be called several times if the name was resolved to multiple addresses, and we failed to connect to the first(s) one(s).
- Parameters:
remoteAddress- the address we try to connect to
-
onTcpConnectSuccess
default void onTcpConnectSuccess(InetSocketAddress remoteAddress, io.netty.channel.Channel connection)
Notify the callback after a successful connect- Parameters:
remoteAddress- the address we try to connect toconnection- the connection
-
onTcpConnectFailure
default void onTcpConnectFailure(InetSocketAddress remoteAddress, Throwable cause)
Notify the callback after a failed connect.Might be called several times, or be followed by onTcpConnectSuccess when the name was resolved to multiple addresses.
- Parameters:
remoteAddress- the address we try to connect tocause- the cause of the failure
-
onTlsHandshakeAttempt
default void onTlsHandshakeAttempt()
Notify the callback before TLS handshake
-
onTlsHandshakeSuccess
default void onTlsHandshakeSuccess(SSLSession sslSession)
Notify the callback after the TLS was successful
-
onTlsHandshakeFailure
default void onTlsHandshakeFailure(Throwable cause)
Notify the callback after the TLS failed- Parameters:
cause- the cause of the failure
-
onConnectionPoolAttempt
default void onConnectionPoolAttempt()
Notify the callback when trying to fetch a connection from the pool.
-
onConnectionPooled
default void onConnectionPooled(io.netty.channel.Channel connection)
Notify the callback when a new connection was successfully fetched from the pool.- Parameters:
connection- the connection
-
onConnectionOffer
default void onConnectionOffer(io.netty.channel.Channel connection)
Notify the callback when trying to offer a connection to the pool.- Parameters:
connection- the connection
-
onRequestSend
default void onRequestSend(NettyRequest request)
Notify the callback when a request is being written on the channel. If the original request causes multiple requests to be sent, for example, because of authorization or retry, it will be notified multiple times.- Parameters:
request- the real request object as passed to the provider
-
onRetry
default void onRetry()
Notify the callback every time a request is being retried.
-
-