Class ThreadedRequestHandler
- java.lang.Object
-
- com.yahoo.jdisc.AbstractResource
-
- com.yahoo.jdisc.handler.AbstractRequestHandler
-
- com.yahoo.jdisc.handler.ThreadedRequestHandler
-
- All Implemented Interfaces:
RequestHandler
,SharedResource
public abstract class ThreadedRequestHandler extends AbstractRequestHandler
This class implements a
RequestHandler
with a synchronoushandleRequest(Request, BufferedContentChannel, ResponseHandler)
API for handlingRequest
s. 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
-
-
Field Summary
-
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)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getTimeout(java.util.concurrent.TimeUnit unit)
Returns the timeout that this ThreadedRequestHandler sets on all handledRequest
s.protected void
handleRequest(Request request, BufferedContentChannel requestContent, ResponseHandler responseHandler)
Override this method if you want to access theRequest
's content using aBufferedContentChannel
.protected void
handleRequest(Request request, ContentInputStream requestContent, ResponseHandler responseHandler)
Implement this method if you want to access theRequest
's content using aContentInputStream
.protected void
handleRequest(Request request, ReadableContentChannel requestContent, ResponseHandler responseHandler)
Implement this method if you want to access theRequest
's content using aReadableContentChannel
.ContentChannel
handleRequest(Request request, ResponseHandler responseHandler)
This method will process the givenRequest
and return aContentChannel
into which the caller can write the Request's content.void
setTimeout(long timeout, java.util.concurrent.TimeUnit unit)
Sets the timeout that this ThreadedRequestHandler sets on all handledRequest
s.-
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
-
-
-
-
Method Detail
-
setTimeout
public final void setTimeout(long timeout, java.util.concurrent.TimeUnit unit)
Sets the timeout that this ThreadedRequestHandler sets on all handled
Request
s. 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
Request
s.- 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 aContentChannel
into which the caller can write the Request's content. For every call to this method, the implementation must call the providedResponseHandler
exactly once.Notice that unless this method throws an Exception, a reference to the currently active
Container
instance is kept internally untilResponseHandler.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 callResponseHandler.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 correspondingResponse
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, BufferedContentChannel requestContent, ResponseHandler responseHandler)
Override this method if you want to access the
Request
's content using aBufferedContentChannel
. If you do not override this method, it will callhandleRequest(Request, ReadableContentChannel, ResponseHandler)
.- Parameters:
request
- The Request to handle.responseHandler
- The handler to pass the correspondingResponse
to.requestContent
- The content of the Request.
-
handleRequest
protected void handleRequest(Request request, ReadableContentChannel requestContent, ResponseHandler responseHandler)
Implement this method if you want to access the
Request
's content using aReadableContentChannel
. If you do not override this method, it will callhandleRequest(Request, ContentInputStream, ResponseHandler)
.- Parameters:
request
- The Request to handle.responseHandler
- The handler to pass the correspondingResponse
to.requestContent
- The content of the Request.
-
handleRequest
protected void handleRequest(Request request, ContentInputStream requestContent, ResponseHandler responseHandler)
Implement this method if you want to access the
Request
's content using aContentInputStream
. If you do not override this method, it will dispatch aResponse
to theResponseHandler
with aResponse.Status.NOT_IMPLEMENTED
status.- Parameters:
request
- The Request to handle.responseHandler
- The handler to pass the correspondingResponse
to.requestContent
- The content of the Request.
-
-