Class 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 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 Detail

      • RequestDispatch

        public RequestDispatch()
    • 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 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.

        Returns:
        The ByteBuffers to write to the Request's ContentChannel.
      • dispatch

        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.

        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 interface com.google.common.util.concurrent.ListenableFuture<Response>
      • cancel

        public final boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<Response>
      • isCancelled

        public final boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<Response>
      • isDone

        public final boolean isDone()
        Specified by:
        isDone in interface java.util.concurrent.Future<Response>
      • get

        public final Response get()
                           throws java.lang.InterruptedException,
                                  java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.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 interface java.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 a ContentChannel into which the caller can write the Response's content.

        Specified by:
        handleResponse in interface ResponseHandler
        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.