Class ThreadedRequestHandler

All Implemented Interfaces:
RequestHandler, SharedResource
Direct Known Subclasses:
ThreadedHttpRequestHandler

public abstract class ThreadedRequestHandler extends AbstractRequestHandler
A request handler which assigns a worker thread to handle each request. This is meant to be subclassed by handlers who do work by executing each request in a separate thread.

Note that this means that subclass handlers are synchronous - the request io can continue after completion of the worker thread.

Author:
Simon Thoresen Hult
  • Field Details

  • Constructor Details

    • ThreadedRequestHandler

      protected ThreadedRequestHandler(Executor executor)
    • ThreadedRequestHandler

      @Inject protected ThreadedRequestHandler(Executor executor, Metric metric)
    • ThreadedRequestHandler

      @Inject protected ThreadedRequestHandler(Executor executor, Metric metric, boolean allowAsyncResponse)
      Creates a threaded request handler
      Parameters:
      executor - the executor to use to execute requests
      metric - the metric object to which event in this are to be collected
      allowAsyncResponse - true to allow the response header to be created asynchronously. If false (default), this will create an error response if the response header is not returned by the subclass of this before handleRequest returns.
  • Method Details

    • handleRequest

      public final ContentChannel handleRequest(Request request, ResponseHandler responseHandler)
      Handles a request by assigning a worker thread to it.
      Throws:
      OverloadException - if thread pool has no available thread
    • getRequestType

      protected Optional<Request.RequestType> getRequestType()

      Returns the request type classification to use for requests to this handler. This overrides the default classification based on request method, and can in turn be overridden by setting a request type on individual responses in handleRequest whenever it is invoked (i.e not for requests that are rejected early e.g due to overload).

      This default implementation returns empty.

      Returns:
      the request type to set, or empty to not override the default classification based on request method
    • getTimeout

      public Duration getTimeout()
    • executor

      public Executor executor()
    • handleRequest

      protected abstract void handleRequest(Request request, BufferedContentChannel requestContent, ResponseHandler responseHandler)
    • writeErrorResponseOnOverload

      protected void writeErrorResponseOnOverload(Request request, ResponseHandler responseHandler)
      Invoked to write an error response when the worker pool is overloaded. A subclass may override this method to define a custom response.