org.restlet.engine.connector
Class BaseHelper<T extends Connector>

java.lang.Object
  extended by org.restlet.engine.Helper
      extended by org.restlet.engine.RestletHelper<T>
          extended by org.restlet.engine.ConnectorHelper<T>
              extended by org.restlet.engine.connector.BaseHelper<T>
Direct Known Subclasses:
ConnectionHelper

public abstract class BaseHelper<T extends Connector>
extends ConnectorHelper<T>

Base connector helper. Here is the list of parameters that are supported. They should be set in the connector's context before it is started:

Parameter name Value type Default value Description
controllerDaemon boolean true (client), false (server) Indicates if the controller thread should be a daemon (not blocking JVM exit).
controllerSleepTimeMs int 60 000 Time for the controller thread to sleep between each control. A value strictly superior to 0 is required.
minThreads int 1 Minimum number of worker threads waiting to service calls, even if they are idle. Technically speaking, this is a core number of threads that are pre-started.
lowThreads int 8 Number of worker threads determining when the connector is considered overloaded. This triggers some protection actions such as not accepting new connections.
maxThreads int 10 Maximum number of worker threads that can service calls. If this number is reached then additional calls are queued if the "maxQueued" value hasn't been reached.
maxQueued int 0 Maximum number of calls that can be queued if there aren't any worker thread available to service them. If the value is '0', then no queue is used and calls are rejected if no worker thread is immediately available. If the value is '-1', then an unbounded queue is used and calls are never rejected.

Note: make sure that this value is consistent with getMinThreads() and the behavior of the ThreadPoolExecutor configured internally.
maxIoIdleTimeMs int 60 000 Maximum time for an idle IO connection or request to wait for an operation before being closed. For an unlimited wait, use '0' as value.
maxThreadIdleTimeMs int 300 000 Time for an idle thread to wait for an operation before being collected.
tracing boolean false Indicates if all messages should be printed on the standard console.
workerThreads boolean true Indicates if the processing of calls should be done via threads provided by a worker service (i.e. a pool of worker threads). Note that if set to false, calls will be processed a single IO selector thread, which should never block, otherwise the other connections would hang.
inboundBufferSize int 16 * 1024 Size of the content buffer for receiving messages.
outboundBufferSize int 32 * 1024 Size of the content buffer for sending messages.
directBuffers boolean false Indicates if direct NIO buffers should be allocated instead of regular buffers. See NIO's ByteBuffer Javadocs. Note that tracing must be disabled to use direct buffers.
throttleTimeMs int 0 Time to wait between socket write operations in milliseconds. Can prevent TCP buffer overflows.
transport String TCP Indicates the transport protocol such as TCP or UDP.

Author:
Jerome Louvel

Field Summary
protected  boolean clientSide
          Indicates if it is helping a client connector.
protected  ConnectionController controller
          The controller task.
protected  Queue<Response> inboundMessages
          The queue of inbound messages.
protected  Queue<Response> outboundMessages
          The queue of outbound messages.
 
Constructor Summary
BaseHelper(T connector, boolean clientSide)
          Constructor.
 
Method Summary
protected  boolean control()
          Controls the helper for inbound or outbound messages to handle.
protected abstract  ConnectionController createController()
          Creates a new controller.
protected  ExecutorService createControllerService()
          Creates the connector controller service.
protected  Request createRequest()
          Creates the request object.
protected  ThreadPoolExecutor createWorkerService()
          Creates the handler service.
protected  void doFinishStop()
          Finish stopping the helper.
protected  void doGracefulStop()
          Do a graceful stop first.
abstract  void doHandleInbound(Response response)
          Effectively handles an inbound message.
abstract  void doHandleOutbound(Response response)
          Effectively handles an outbound message.
protected  void execute(Runnable task)
          Executes the next task in a separate thread provided by the worker service, only if the worker service isn't busy.
 ConnectionController getController()
          Returns the controller task.
 int getControllerSleepTimeMs()
          Returns the time for the controller thread to sleep between each control.
 int getInboundBufferSize()
          Returns the size of the content buffer for receiving messages.
protected  Queue<Response> getInboundMessages()
          Returns the queue of inbound messages pending for handling.
 int getLowThreads()
          Returns the number of threads for the overload state.
 int getMaxIoIdleTimeMs()
          Returns the time for an idle IO connection or request to wait for an operation before being closed.
 int getMaxQueued()
          Returns the maximum number of calls that can be queued if there aren't any worker thread available to service them.
 int getMaxThreadIdleTimeMs()
          Returns the time for an idle thread to wait for an operation before being collected.
 int getMaxThreads()
          Returns the maximum threads that will service requests.
 int getMinThreads()
          Returns the minimum threads waiting to service requests.
 int getOutboundBufferSize()
          Returns the size of the content buffer for sending responses.
protected  Queue<Response> getOutboundMessages()
          Returns the queue of outbound messages pending for handling.
 Request getRequest(Response response)
          Returns the parent request of this response.
 int getThrottleTimeMs()
          Returns the time to wait between socket write operations in milliseconds.
 OutputStream getTraceStream()
          Returns the trace output stream to use if tracing is enabled.
 String getTransport()
          Returns the transport protocol.
 ThreadPoolExecutor getWorkerService()
          Returns the connection handler service.
protected abstract  void handleInbound(Response response)
          Handle the given inbound message.
protected  void handleInbound(Response response, boolean synchronous)
          Handle the given inbound message.
protected abstract  void handleOutbound(Response response)
          Handle the given outbound message.
protected  void handleOutbound(Response response, boolean synchronous)
          Handle the given outbound message.
 boolean hasWorkerThreads()
          Indicates if the worker service (pool of worker threads) is enabled.
 boolean isClientSide()
          Indicates if it is helping a client connector.
abstract  boolean isControllerDaemon()
          Indicates if the controller thread should be a daemon (not blocking JVM exit).
 boolean isDirectBuffers()
          Indicates if direct NIO buffers should be used.
 boolean isServerSide()
          Indicates if it is helping a server connector.
 boolean isTracing()
          Indicates if console tracing is enabled.
protected  boolean isWorkerServiceOverloaded()
          Indicates if the worker service is busy.
 void onInboundError(Status status, Response message)
          Called on error.
 void onOutboundError(Status status, Response message)
          Called on error.
 void start()
          Start callback.
 void stop()
          Stop callback.
 void traceWorkerService()
          Adds traces on the worker service.
 
Methods inherited from class org.restlet.engine.ConnectorHelper
getConnectorService, getContext, getProtocols, update
 
Methods inherited from class org.restlet.engine.RestletHelper
getAttributes, getHelped, getHelpedParameters, getLogger, getMetadataService, handle, setHelped
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientSide

protected final boolean clientSide
Indicates if it is helping a client connector.


controller

protected final ConnectionController controller
The controller task.


inboundMessages

protected final Queue<Response> inboundMessages
The queue of inbound messages.


outboundMessages

protected final Queue<Response> outboundMessages
The queue of outbound messages.

Constructor Detail

BaseHelper

public BaseHelper(T connector,
                  boolean clientSide)
Constructor.

Parameters:
connector - The helped connector.
clientSide - True if it is helping a client connector.
Method Detail

control

protected boolean control()
Controls the helper for inbound or outbound messages to handle.

Returns:
Indicates if some concrete activity occurred.

createController

protected abstract ConnectionController createController()
Creates a new controller.

Returns:
A new controller.

createControllerService

protected ExecutorService createControllerService()
Creates the connector controller service.

Returns:
The connector controller service.

createRequest

protected Request createRequest()
Creates the request object.

Returns:
The request object.

createWorkerService

protected ThreadPoolExecutor createWorkerService()
Creates the handler service.

Returns:
The handler service.

doFinishStop

protected void doFinishStop()
Finish stopping the helper.


doGracefulStop

protected void doGracefulStop()
Do a graceful stop first.


doHandleInbound

public abstract void doHandleInbound(Response response)
Effectively handles an inbound message.

Parameters:
response - The response to handle.

doHandleOutbound

public abstract void doHandleOutbound(Response response)
Effectively handles an outbound message.

Parameters:
response - The response to handle.

execute

protected void execute(Runnable task)
Executes the next task in a separate thread provided by the worker service, only if the worker service isn't busy.

Parameters:
task - The next task to execute.

getController

public ConnectionController getController()
Returns the controller task.

Returns:
The controller task.

getControllerSleepTimeMs

public int getControllerSleepTimeMs()
Returns the time for the controller thread to sleep between each control.

Returns:
The time for the controller thread to sleep between each control.

getInboundBufferSize

public int getInboundBufferSize()
Returns the size of the content buffer for receiving messages.

Returns:
The size of the content buffer for receiving messages.

getInboundMessages

protected Queue<Response> getInboundMessages()
Returns the queue of inbound messages pending for handling.

Returns:
The queue of inbound messages.

getLowThreads

public int getLowThreads()
Returns the number of threads for the overload state.

Returns:
The number of threads for the overload state.

getMaxIoIdleTimeMs

public int getMaxIoIdleTimeMs()
Returns the time for an idle IO connection or request to wait for an operation before being closed. For an unlimited wait, use '0' as value.

Returns:
The time for an idle IO connection to wait for an operation before being closed.

getMaxQueued

public int getMaxQueued()
Returns the maximum number of calls that can be queued if there aren't any worker thread available to service them. If the value is '0', then no queue is used and calls are rejected if no worker thread is immediately available. If the value is '-1', then an unbounded queue is used and calls are never rejected.

Note: make sure that this value is consistent with getMinThreads() and the behavior of the ThreadPoolExecutor configured internally.

Returns:
The maximum number of calls that can be queued.

getMaxThreadIdleTimeMs

public int getMaxThreadIdleTimeMs()
Returns the time for an idle thread to wait for an operation before being collected.

Returns:
The time for an idle thread to wait for an operation before being collected.

getMaxThreads

public int getMaxThreads()
Returns the maximum threads that will service requests.

Returns:
The maximum threads that will service requests.

getMinThreads

public int getMinThreads()
Returns the minimum threads waiting to service requests. Technically speaking, this is a core number of threads that are pre-started.

Returns:
The minimum threads waiting to service requests.

getOutboundBufferSize

public int getOutboundBufferSize()
Returns the size of the content buffer for sending responses.

Returns:
The size of the content buffer for sending responses.

getOutboundMessages

protected Queue<Response> getOutboundMessages()
Returns the queue of outbound messages pending for handling.

Returns:
The queue of outbound messages.

getRequest

public Request getRequest(Response response)
Returns the parent request of this response.

Parameters:
response - The response to analyze.
Returns:
The parent request if available.

getThrottleTimeMs

public int getThrottleTimeMs()
Returns the time to wait between socket write operations in milliseconds. Can prevent TCP buffer overflows.

Returns:
The time to wait between socket write operations in milliseconds.

getTraceStream

public OutputStream getTraceStream()
Returns the trace output stream to use if tracing is enabled.

Returns:
The trace output stream to use if tracing is enabled.

getTransport

public String getTransport()
Returns the transport protocol.

Returns:
The transport protocol.

getWorkerService

public ThreadPoolExecutor getWorkerService()
Returns the connection handler service.

Returns:
The connection handler service.

handleInbound

protected abstract void handleInbound(Response response)
Handle the given inbound message.

Parameters:
response - The message to handle.

handleInbound

protected void handleInbound(Response response,
                             boolean synchronous)
Handle the given inbound message.

Parameters:
response - The message to handle.
synchronous - True if the current thread should be used.

handleOutbound

protected abstract void handleOutbound(Response response)
Handle the given outbound message.

Parameters:
response - The message to handle.

handleOutbound

protected void handleOutbound(Response response,
                              boolean synchronous)
Handle the given outbound message.

Parameters:
response - The message to handle.
synchronous - True if the current thread should be used.

hasWorkerThreads

public boolean hasWorkerThreads()
Indicates if the worker service (pool of worker threads) is enabled.

Returns:
True if the worker service (pool of worker threads) is enabled.

isClientSide

public boolean isClientSide()
Indicates if it is helping a client connector.

Returns:
True if it is helping a client connector.

isControllerDaemon

public abstract boolean isControllerDaemon()
Indicates if the controller thread should be a daemon (not blocking JVM exit).

Returns:
True if the controller thread should be a daemon (not blocking JVM exit).

isDirectBuffers

public boolean isDirectBuffers()
Indicates if direct NIO buffers should be used. Note that tracing must be disabled to use direct buffers.

Returns:
True if direct NIO buffers should be used.

isServerSide

public boolean isServerSide()
Indicates if it is helping a server connector.

Returns:
True if it is helping a server connector.

isTracing

public boolean isTracing()
Indicates if console tracing is enabled.

Returns:
True if console tracing is enabled.

isWorkerServiceOverloaded

protected boolean isWorkerServiceOverloaded()
Indicates if the worker service is busy. This state is detected by checking if the number of active task running is superior or equal to the maximum pool size.

Returns:
True if the worker service is busy.

onInboundError

public void onInboundError(Status status,
                           Response message)
Called on error. Unblocks the message.

Parameters:
status - The error status to set on the responses.
message - The message to unblock.

onOutboundError

public void onOutboundError(Status status,
                            Response message)
Called on error. Unblocks the message.

Parameters:
status - The error status to set on the responses.
message - The message to unblock.

start

public void start()
           throws Exception
Description copied from class: RestletHelper
Start callback.

Overrides:
start in class ConnectorHelper<T extends Connector>
Throws:
Exception

stop

public void stop()
          throws Exception
Description copied from class: RestletHelper
Stop callback.

Overrides:
stop in class ConnectorHelper<T extends Connector>
Throws:
Exception

traceWorkerService

public void traceWorkerService()
Adds traces on the worker service.



Copyright © 2005-2013 Restlet.