Class ThreadedRequestHandler

  • All Implemented Interfaces:
    RequestHandler, SharedResource

    public abstract class ThreadedRequestHandler
    extends AbstractRequestHandler

    This class implements a RequestHandler with a synchronous handleRequest(Request, BufferedContentChannel, ResponseHandler) API for handling Requests. An Executor is provided at construction time, and all Requests are automatically scheduled for processing on that Executor.

    A very simple echo handler could be implemented like this:

     class MyRequestHandler extends ThreadedRequestHandler {
    
         @Inject
         MyRequestHandler(Executor executor) {
             super(executor);
         }
    
         @Override
         protected void handleRequest(Request request, ReadableContentChannel requestContent, ResponseHandler handler) {
             ContentWriter responseContent = ResponseDispatch.newInstance(Response.Status.OK).connectWriter(handler);
             try {
                 for (ByteBuffer buf : requestContent) {
                     responseContent.write(buf);
                 }
             } catch (RuntimeException e) {
                 requestContent.failed(e);
                 throw e;
             } finally {
                 responseContent.close();
             }
         }
     }
     
    Author:
    Simon Thoresen Hult
    • Constructor Detail

      • ThreadedRequestHandler

        protected ThreadedRequestHandler​(java.util.concurrent.Executor executor)
    • Method Detail

      • setTimeout

        public final void setTimeout​(long timeout,
                                     java.util.concurrent.TimeUnit unit)

        Sets the timeout that this ThreadedRequestHandler sets on all handled Requests. If the timeout value is less than or equal to zero, no timeout will be applied.

        Parameters:
        timeout - The allocated amount of time.
        unit - The time unit of the timeout argument.
      • getTimeout

        public final long getTimeout​(java.util.concurrent.TimeUnit unit)

        Returns the timeout that this ThreadedRequestHandler sets on all handled Requests.

        Parameters:
        unit - The unit to use for the return value.
        Returns:
        The timeout in the appropriate unit.
      • handleRequest

        public final ContentChannel handleRequest​(Request request,
                                                  ResponseHandler responseHandler)
        Description copied from interface: RequestHandler

        This method will process the given Request and return a ContentChannel into which the caller can write the Request's content. For every call to this method, the implementation must call the provided ResponseHandler exactly once.

        Notice that unless this method throws an Exception, a reference to the currently active Container instance is kept internally until ResponseHandler.handleResponse(Response) has been called. This ensures that the configured environment of the Request is stable throughout its lifetime. Failure to call back with a Response will prevent the release of that reference, and therefore prevent the corresponding Container from ever shutting down. The requirement to call ResponseHandler.handleResponse(Response) is regardless of any subsequent errors that may occur while working with the returned ContentChannel.

        Parameters:
        request - The Request to handle.
        responseHandler - The handler to pass the corresponding Response to.
        Returns:
        The ContentChannel to write the Request content to. Notice that the ContentChannel itself also holds a Container reference, so failure to close this will prevent the Container from ever shutting down.
      • handleRequest

        protected void handleRequest​(Request request,
                                     ContentInputStream requestContent,
                                     ResponseHandler responseHandler)

        Implement this method if you want to access the Request's content using a ContentInputStream. If you do not override this method, it will dispatch a Response to the ResponseHandler with a Response.Status.NOT_IMPLEMENTED status.

        Parameters:
        request - The Request to handle.
        responseHandler - The handler to pass the corresponding Response to.
        requestContent - The content of the Request.