Class RequestDispatch
- All Implemented Interfaces:
ResponseHandler
,Future<Response>
- Direct Known Subclasses:
CallableRequestDispatch
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(); }
- Author:
- Simon Thoresen Hult
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(Runnable listener, Executor executor) final boolean
cancel
(boolean mayInterruptIfRunning) final ContentChannel
connect()
This methods callsnewRequest()
to create a newRequest
, and then callsRequest.connect(ResponseHandler)
on that.final FastContentWriter
This is a convenient method to construct aFastContentWriter
over theContentChannel
returned by callingconnect()
.final CompletableFuture<Response>
dispatch()
This method callsconnect()
to establish aContentChannel
for theRequest
, and then iterates through all the ByteBuffers returned byrequestContent()
and writes them to that ContentChannel.final Response
get()
final Response
handleResponse
(Response response) This method will process the givenResponse
and return aContentChannel
into which the caller can write the Response's content.final boolean
final boolean
isDone()
protected abstract Request
Creates and returns theRequest
to dispatch.protected Iterable<ByteBuffer>
Returns an Iterable for the ByteBuffers that thedispatch()
method should write to theRequest
once it hasconnected
.
-
Constructor Details
-
RequestDispatch
public RequestDispatch()
-
-
Method Details
-
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.- Returns:
- The Request to dispatch.
-
requestContent
Returns an Iterable for the ByteBuffers that the
dispatch()
method should write to theRequest
once it hasconnected
. 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.- Returns:
- The ByteBuffers to write to the Request's ContentChannel.
-
connect
This methods calls
newRequest()
to create a newRequest
, and then callsRequest.connect(ResponseHandler)
on that. This method uses afinally
block to make sure that the Request is alwaysreleased
.- Returns:
- The ContentChannel to write the Request's content to.
-
connectFastWriter
This is a convenient method to construct a
FastContentWriter
over theContentChannel
returned by callingconnect()
.- Returns:
- The ContentWriter for the connected Request.
-
dispatch
This method calls
connect()
to establish aContentChannel
for theRequest
, and then iterates through all the ByteBuffers returned byrequestContent()
and writes them to that ContentChannel. This method uses afinally
block to make sure that the ContentChannel is alwaysclosed
.The returned Future will wait for all CompletionHandlers associated with the Request have been completed, and a
Response
has been received.- Returns:
- A Future that can be waited for.
-
addListener
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning) -
isCancelled
public final boolean isCancelled()- Specified by:
isCancelled
in interfaceFuture<Response>
-
isDone
public final boolean isDone() -
get
- Specified by:
get
in interfaceFuture<Response>
- Throws:
InterruptedException
ExecutionException
-
get
public final Response get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
get
in interfaceFuture<Response>
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
handleResponse
Description copied from interface:ResponseHandler
This method will process the givenResponse
and return aContentChannel
into which the caller can write the Response's content.- Specified by:
handleResponse
in interfaceResponseHandler
- Parameters:
response
- the Response to handle- Returns:
- the ContentChannel to write the Response content to. Notice that the ContentChannel holds a Container reference, so failure to close this will prevent the Container from ever shutting down.
-