Class RequestDispatch
- java.lang.Object
-
- com.yahoo.jdisc.handler.RequestDispatch
-
- All Implemented Interfaces:
com.google.common.util.concurrent.ListenableFuture<Response>
,ResponseHandler
,java.util.concurrent.Future<Response>
- Direct Known Subclasses:
CallableRequestDispatch
public abstract class RequestDispatch extends java.lang.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 theSharedResource
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 aNullContent
. If you want to return a differentContentChannel
, you need to overridehandleResponse(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
Constructors Constructor Description RequestDispatch()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
boolean
cancel(boolean mayInterruptIfRunning)
ContentChannel
connect()
This methods callsnewRequest()
to create a newRequest
, and then callsRequest.connect(ResponseHandler)
on that.FastContentWriter
connectFastWriter()
This is a convenient method to construct aFastContentWriter
over theContentChannel
returned by callingconnect()
.com.google.common.util.concurrent.ListenableFuture<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.Response
get()
Response
get(long timeout, java.util.concurrent.TimeUnit unit)
ContentChannel
handleResponse(Response response)
This method will process the givenResponse
and return aContentChannel
into which the caller can write the Response's content.boolean
isCancelled()
boolean
isDone()
protected abstract Request
newRequest()
Creates and returns theRequest
to dispatch.protected java.lang.Iterable<java.nio.ByteBuffer>
requestContent()
Returns an Iterable for the ByteBuffers that thedispatch()
method should write to theRequest
once it hasconnected
.
-
-
-
Method Detail
-
newRequest
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.- Returns:
- The Request to dispatch.
-
requestContent
protected java.lang.Iterable<java.nio.ByteBuffer> 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
public final 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
public final FastContentWriter connectFastWriter()
This is a convenient method to construct a
FastContentWriter
over theContentChannel
returned by callingconnect()
.- Returns:
- The ContentWriter for the connected Request.
-
dispatch
public final com.google.common.util.concurrent.ListenableFuture<Response> 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
public void addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
- Specified by:
addListener
in interfacecom.google.common.util.concurrent.ListenableFuture<Response>
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning)
- Specified by:
cancel
in interfacejava.util.concurrent.Future<Response>
-
isCancelled
public final boolean isCancelled()
- Specified by:
isCancelled
in interfacejava.util.concurrent.Future<Response>
-
isDone
public final boolean isDone()
- Specified by:
isDone
in interfacejava.util.concurrent.Future<Response>
-
get
public final Response get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
- Specified by:
get
in interfacejava.util.concurrent.Future<Response>
- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
-
get
public final Response get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
- Specified by:
get
in interfacejava.util.concurrent.Future<Response>
- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
-
handleResponse
public ContentChannel handleResponse(Response response)
Description copied from interface:ResponseHandler
This method will process the given
Response
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.
-
-