Class ThreadedRequestHandler

  • All Implemented Interfaces:
    com.yahoo.jdisc.handler.RequestHandler, com.yahoo.jdisc.SharedResource
    Direct Known Subclasses:
    ThreadedHttpRequestHandler

    public abstract class ThreadedRequestHandler
    extends com.yahoo.jdisc.handler.AbstractRequestHandler
    A request handler which assigns a worker thread to handle each request. This is mean to be subclasses by handlers who does 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 Summary

      Fields 
      Modifier and Type Field Description
      protected com.yahoo.jdisc.Metric metric  
      • Fields inherited from interface com.yahoo.jdisc.SharedResource

        DEBUG, SYSTEM_PROPERTY_NAME_DEBUG
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ThreadedRequestHandler​(java.util.concurrent.Executor executor)  
      protected ThreadedRequestHandler​(java.util.concurrent.Executor executor, com.yahoo.jdisc.Metric metric)  
      protected ThreadedRequestHandler​(java.util.concurrent.Executor executor, com.yahoo.jdisc.Metric metric, boolean allowAsyncResponse)
      Creates a threaded request handler
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.Executor executor()  
      protected java.util.Optional<com.yahoo.jdisc.Request.RequestType> getRequestType()
      Returns the request type classification to use for requests to this handler.
      java.time.Duration getTimeout()  
      protected abstract void handleRequest​(com.yahoo.jdisc.Request request, com.yahoo.jdisc.handler.BufferedContentChannel requestContent, com.yahoo.jdisc.handler.ResponseHandler responseHandler)  
      com.yahoo.jdisc.handler.ContentChannel handleRequest​(com.yahoo.jdisc.Request request, com.yahoo.jdisc.handler.ResponseHandler responseHandler)
      Handles a request by assigning a worker thread to it.
      protected void writeErrorResponseOnOverload​(com.yahoo.jdisc.Request request, com.yahoo.jdisc.handler.ResponseHandler responseHandler)
      Invoked to write an error response when the worker pool is overloaded.
      • Methods inherited from class com.yahoo.jdisc.handler.AbstractRequestHandler

        handleTimeout
      • Methods inherited from class com.yahoo.jdisc.AbstractResource

        currentState, destroy, refer, release, retainCount
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface com.yahoo.jdisc.SharedResource

        refer, release
    • Field Detail

      • metric

        protected final com.yahoo.jdisc.Metric metric
    • Constructor Detail

      • ThreadedRequestHandler

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

        @Inject
        protected ThreadedRequestHandler​(java.util.concurrent.Executor executor,
                                         com.yahoo.jdisc.Metric metric)
      • ThreadedRequestHandler

        @Inject
        protected ThreadedRequestHandler​(java.util.concurrent.Executor executor,
                                         com.yahoo.jdisc.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 Detail

      • handleRequest

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

        protected java.util.Optional<com.yahoo.jdisc.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 java.time.Duration getTimeout()
      • executor

        public java.util.concurrent.Executor executor()
      • handleRequest

        protected abstract void handleRequest​(com.yahoo.jdisc.Request request,
                                              com.yahoo.jdisc.handler.BufferedContentChannel requestContent,
                                              com.yahoo.jdisc.handler.ResponseHandler responseHandler)
      • writeErrorResponseOnOverload

        protected void writeErrorResponseOnOverload​(com.yahoo.jdisc.Request request,
                                                    com.yahoo.jdisc.handler.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.