public class TimedResponseListener extends Object implements Response.Listener, Schedulable, Runnable
Response.Listener
that allows to specify a timeout for asynchronous
operations.
TimedResponseListener
may be used to decorate a delegate Response.CompleteListener
provided by the application. Events are forwarded by TimedResponseListener
to the delegate
listener.
Alternatively, TimedResponseListener
may be subclassed to override callbacks that are
interesting to the application, typically onComplete(Result)
.
If the timeout specified at the constructor elapses, the request is aborted
with a TimeoutException
.
Typical usage is:
Request request = httpClient.newRequest(...)...; TimedResponseListener listener = new TimedResponseListener(5, TimeUnit.SECONDS, request, new Response.CompleteListener() { public void onComplete(Result result) { // Invoked when request/response completes or when timeout elapses // Your logic here } }); request.send(listener); // Asynchronous send
Response.Listener.Empty
Constructor and Description |
---|
TimedResponseListener(long timeout,
TimeUnit unit,
Request request) |
TimedResponseListener(long timeout,
TimeUnit unit,
Request request,
Response.CompleteListener delegate) |
Modifier and Type | Method and Description |
---|---|
boolean |
cancel() |
void |
onBegin(Response response)
Callback method invoked when the response line containing HTTP version,
HTTP status code and reason has been received and parsed.
|
void |
onComplete(Result result)
Callback method invoked when the request and the response have been processed,
either successfully or not.
|
void |
onContent(Response response,
ByteBuffer content)
Callback method invoked when the response content has been received.
|
void |
onFailure(Response response,
Throwable failure)
Callback method invoked when the response has failed in the process of being received
|
boolean |
onHeader(Response response,
HttpField field)
Callback method invoked when a response header has been received,
returning whether the header should be processed or not.
|
void |
onHeaders(Response response)
Callback method invoked when the response headers have been received and parsed.
|
void |
onSuccess(Response response)
Callback method invoked when the whole response has been successfully received.
|
void |
run() |
boolean |
schedule(Scheduler scheduler) |
public TimedResponseListener(long timeout, TimeUnit unit, Request request, Response.CompleteListener delegate)
public void onBegin(Response response)
Response.BeginListener
onBegin
in interface Response.BeginListener
response
- the response containing the response line datapublic boolean onHeader(Response response, HttpField field)
Response.HeaderListener
onHeader
in interface Response.HeaderListener
response
- the response containing the response line data and the headers so farfield
- the header receivedpublic void onHeaders(Response response)
Response.HeadersListener
onHeaders
in interface Response.HeadersListener
response
- the response containing the response line data and the headerspublic void onContent(Response response, ByteBuffer content)
Response.ContentListener
content
buffer must be consumed
before returning from this method.onContent
in interface Response.ContentListener
response
- the response containing the response line data and the headerscontent
- the content bytes receivedpublic void onSuccess(Response response)
Response.SuccessListener
onSuccess
in interface Response.SuccessListener
response
- the response containing the response line data and the headerspublic void onFailure(Response response, Throwable failure)
Response.FailureListener
onFailure
in interface Response.FailureListener
response
- the response containing data up to the point the failure happenedfailure
- the failure happenedpublic void onComplete(Result result)
Response.CompleteListener
result
parameter contains the request, the response, and eventual failures.
Requests may complete after response, for example in case of big uploads that are
discarded or read asynchronously by the server.
This method is always invoked after Response.SuccessListener.onSuccess(Response)
or
Response.FailureListener.onFailure(Response, Throwable)
, and only when request indicates that
it is completed.onComplete
in interface Response.CompleteListener
result
- the result of the request / response exchangepublic boolean schedule(Scheduler scheduler)
schedule
in interface Schedulable
public boolean cancel()
cancel
in interface Schedulable
Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.