public abstract class ThreadedRequestHandler extends AbstractRequestHandler
This class implements a RequestHandler
with a synchronous handleRequest(Request,
BufferedContentChannel, ResponseHandler)
API for handling Request
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(); } } }
Modifier and Type | Class and Description |
---|---|
private class |
ThreadedRequestHandler.RequestTask |
Modifier and Type | Field and Description |
---|---|
private Executor |
executor |
private long |
timeout |
DEBUG, SYSTEM_PROPERTY_NAME_DEBUG
Modifier | Constructor and Description |
---|---|
protected |
ThreadedRequestHandler(Executor executor) |
Modifier and Type | Method and Description |
---|---|
long |
getTimeout(TimeUnit unit)
Returns the timeout that this ThreadedRequestHandler sets on all handled
Request s. |
protected void |
handleRequest(Request request,
BufferedContentChannel requestContent,
ResponseHandler responseHandler)
Override this method if you want to access the
Request 's content using a BufferedContentChannel . |
protected void |
handleRequest(Request request,
ContentInputStream requestContent,
ResponseHandler responseHandler)
Implement this method if you want to access the
Request 's content using a ContentInputStream . |
protected void |
handleRequest(Request request,
ReadableContentChannel requestContent,
ResponseHandler responseHandler)
Implement this method if you want to access the
Request 's content using a ReadableContentChannel . |
ContentChannel |
handleRequest(Request request,
ResponseHandler responseHandler)
This method will process the given
Request and return a ContentChannel into which the caller
can write the Request's content. |
void |
setTimeout(long timeout,
TimeUnit unit)
Sets the timeout that this ThreadedRequestHandler sets on all handled
Request s. |
handleTimeout
currentState, destroy, refer, release, retainCount
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
refer, release
private final Executor executor
private volatile long timeout
protected ThreadedRequestHandler(Executor executor)
public final void setTimeout(long timeout, 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.
timeout
- The allocated amount of time.unit
- The time unit of the timeout argument.public final long getTimeout(TimeUnit unit)
Returns the timeout that this ThreadedRequestHandler sets on all handled Request
s.
unit
- The unit to use for the return value.public final ContentChannel handleRequest(Request request, ResponseHandler responseHandler)
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.
request
- The Request to handle.responseHandler
- The handler to pass the corresponding Response
to.protected void handleRequest(Request request, BufferedContentChannel requestContent, ResponseHandler responseHandler)
Override this method if you want to access the Request
's content using a BufferedContentChannel
. If you do not override this method, it will call handleRequest(Request,
ReadableContentChannel, ResponseHandler)
.
request
- The Request to handle.responseHandler
- The handler to pass the corresponding Response
to.requestContent
- The content of the Request.protected void handleRequest(Request request, ReadableContentChannel requestContent, ResponseHandler responseHandler)
Implement this method if you want to access the Request
's content using a ReadableContentChannel
. If you do not override this method, it will call handleRequest(Request,
ContentInputStream, ResponseHandler)
.
request
- The Request to handle.responseHandler
- The handler to pass the corresponding Response
to.requestContent
- The content of the Request.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.
request
- The Request to handle.responseHandler
- The handler to pass the corresponding Response
to.requestContent
- The content of the Request.Copyright © 2018. All rights reserved.