Class ExecutionHandler
java.lang.Object
org.jboss.netty.handler.execution.ExecutionHandler
- All Implemented Interfaces:
ChannelDownstreamHandler,ChannelHandler,ChannelUpstreamHandler,ExternalResourceReleasable
@Sharable
public class ExecutionHandler
extends Object
implements ChannelUpstreamHandler, ChannelDownstreamHandler, ExternalResourceReleasable
Forwards an upstream Using other
Although it's recommended to use
ChannelEvent to an Executor.
ExecutionHandler is often used when your ChannelHandler
performs a blocking operation that takes long time or accesses a resource
which is not CPU-bound business logic such as DB access. Running such
operations in a pipeline without an ExecutionHandler will result in
unwanted hiccup during I/O because an I/O thread cannot perform I/O until
your handler returns the control to the I/O thread.
In most cases, an ExecutionHandler is coupled with an
OrderedMemoryAwareThreadPoolExecutor because it guarantees the
correct event execution order and prevents an OutOfMemoryError
under load:
public class DatabaseGatewayPipelineFactory implementsPlease refer toChannelPipelineFactory{ private finalExecutionHandlerexecutionHandler; public DatabaseGatewayPipelineFactory(ExecutionHandlerexecutionHandler) { this.executionHandler = executionHandler; } publicChannelPipelinegetPipeline() { returnChannels.pipeline( new DatabaseGatewayProtocolEncoder(), new DatabaseGatewayProtocolDecoder(), executionHandler, // Must be shared new DatabaseQueryingHandler()); } } ... public static void main(String[] args) {ServerBootstrapbootstrap = ...; ...ExecutionHandlerexecutionHandler = newExecutionHandler( newOrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576)) bootstrap.setPipelineFactory( new DatabaseGatewayPipelineFactory(executionHandler)); ... bootstrap.bind(...); ... while (!isServerReadyToShutDown()) { // ... wait ... } bootstrap.releaseExternalResources(); executionHandler.releaseExternalResources(); }
OrderedMemoryAwareThreadPoolExecutor for the
detailed information about how the event order is guaranteed.
SEDA (Staged Event-Driven Architecture)
You can implement an alternative thread model such as SEDA by adding more than oneExecutionHandler to the pipeline.
Using other Executor implementation
Although it's recommended to use OrderedMemoryAwareThreadPoolExecutor,
you can use other Executor implementations. However, you must note
that other Executor implementation might break your application
because they often do not maintain event execution order nor interact with
I/O threads to control the incoming traffic and avoid OutOfMemoryError.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Executorprivate final booleanprivate final boolean -
Constructor Summary
ConstructorsConstructorDescriptionExecutionHandler(Executor executor) Creates a new instance with the specifiedExecutor.ExecutionHandler(Executor executor, boolean handleDownstream, boolean handleUpstream) Creates a new instance with the specifiedExecutor. -
Method Summary
Modifier and TypeMethodDescriptionReturns theExecutorwhich was specified with the constructor.voidHandles the specified downstream event.protected booleanHandle suspended readsvoidhandleUpstream(ChannelHandlerContext context, ChannelEvent e) Handles the specified upstream event.voidShuts down theExecutorwhich was specified with the constructor and wait for its termination.
-
Field Details
-
executor
-
handleDownstream
private final boolean handleDownstream -
handleUpstream
private final boolean handleUpstream
-
-
Constructor Details
-
ExecutionHandler
Creates a new instance with the specifiedExecutor. Specify anOrderedMemoryAwareThreadPoolExecutorif unsure. -
ExecutionHandler
Creates a new instance with the specifiedExecutor. Specify anOrderedMemoryAwareThreadPoolExecutorif unsure.
-
-
Method Details
-
getExecutor
Returns theExecutorwhich was specified with the constructor. -
releaseExternalResources
public void releaseExternalResources()Shuts down theExecutorwhich was specified with the constructor and wait for its termination.- Specified by:
releaseExternalResourcesin interfaceExternalResourceReleasable
-
handleUpstream
Description copied from interface:ChannelUpstreamHandlerHandles the specified upstream event.- Specified by:
handleUpstreamin interfaceChannelUpstreamHandler- Parameters:
context- the context object for this handlere- the upstream event to process or intercept- Throws:
Exception
-
handleDownstream
Description copied from interface:ChannelDownstreamHandlerHandles the specified downstream event.- Specified by:
handleDownstreamin interfaceChannelDownstreamHandler- Parameters:
ctx- the context object for this handlere- the downstream event to process or intercept- Throws:
Exception
-
handleReadSuspend
Handle suspended reads
-