public abstract class RequestDispatch extends Object implements com.google.common.util.concurrent.ListenableFuture<Response>, ResponseHandler
This class provides a convenient way of safely dispatching a Request
. Using this class you do not have to
worry about the exception safety surrounding the SharedResource
logic. The internal mechanics of this class
will ensure that anything that goes wrong during dispatch is safely handled according to jDISC contracts.
It also provides a default implementation of the ResponseHandler
interface that returns a NullContent
. If you want to return a different ContentChannel
, you need to override handleResponse(Response)
.
The following is a simple example on how to use this class:
public void handleRequest(final Request parent, final ResponseHandler handler) { new RequestDispatch() { @Override protected Request newRequest() { return new Request(parent, URI.create("http://remotehost/")); } @Override protected Iterable<ByteBuffer> requestContent() { return Collections.singleton(ByteBuffer.wrap(new byte[] { 6, 9 })); } @Override public ContentChannel handleResponse(Response response) { return handler.handleResponse(response); } }.dispatch(); }
Constructor and Description |
---|
RequestDispatch() |
Modifier and Type | Method and Description |
---|---|
void |
addListener(Runnable listener,
Executor executor) |
boolean |
cancel(boolean mayInterruptIfRunning) |
ContentChannel |
connect()
This methods calls
newRequest() to create a new Request , and then calls Request.connect(ResponseHandler) on that. |
FastContentWriter |
connectFastWriter()
This is a convenient method to construct a
FastContentWriter over the ContentChannel returned by
calling connect() . |
com.google.common.util.concurrent.ListenableFuture<Response> |
dispatch()
This method calls
connect() to establish a ContentChannel for the Request , and then
iterates through all the ByteBuffers returned by requestContent() and writes them to that
ContentChannel. |
Response |
get() |
Response |
get(long timeout,
TimeUnit unit) |
ContentChannel |
handleResponse(Response response)
This method will process the given
Response and return a ContentChannel into which the caller
can write the Response's content. |
boolean |
isCancelled() |
boolean |
isDone() |
protected abstract Request |
newRequest()
Creates and returns the
Request to dispatch. |
protected Iterable<ByteBuffer> |
requestContent()
Returns an Iterable for the ByteBuffers that the
dispatch() method should write to the Request once it has connected . |
protected abstract Request newRequest()
Creates and returns the Request
to dispatch. The internal code that calls this method takes care of
the necessary exception safety of connecting the Request.
protected Iterable<ByteBuffer> requestContent()
Returns an Iterable for the ByteBuffers that the dispatch()
method should write to the Request
once it has connected
. The default implementation returns an empty list. Because this
method uses the Iterable interface, you can create the ByteBuffers lazily, or provide them as they become
available.
public final ContentChannel connect()
This methods calls newRequest()
to create a new Request
, and then calls Request.connect(ResponseHandler)
on that. This method uses a finally block to make sure that the
Request is always released
.
public final FastContentWriter connectFastWriter()
This is a convenient method to construct a FastContentWriter
over the ContentChannel
returned by
calling connect()
.
public final com.google.common.util.concurrent.ListenableFuture<Response> dispatch()
This method calls connect()
to establish a ContentChannel
for the Request
, and then
iterates through all the ByteBuffers returned by requestContent()
and writes them to that
ContentChannel. This method uses a finally block to make sure that the ContentChannel is always closed
.
The returned Future will wait for all CompletionHandlers associated with the Request have been completed, and
a Response
has been received.
public void addListener(Runnable listener, Executor executor)
addListener
in interface com.google.common.util.concurrent.ListenableFuture<Response>
public final boolean cancel(boolean mayInterruptIfRunning)
public final boolean isCancelled()
isCancelled
in interface Future<Response>
public final Response get() throws InterruptedException, ExecutionException
get
in interface Future<Response>
InterruptedException
ExecutionException
public final Response get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
get
in interface Future<Response>
InterruptedException
ExecutionException
TimeoutException
public ContentChannel handleResponse(Response response)
ResponseHandler
This method will process the given Response
and return a ContentChannel
into which the caller
can write the Response's content.
handleResponse
in interface ResponseHandler
response
- The Response to handle.Copyright © 2018. All rights reserved.