Class ExecutionHandler
- java.lang.Object
-
- org.jboss.netty.handler.execution.ExecutionHandler
-
- All Implemented Interfaces:
ChannelDownstreamHandler,ChannelHandler,ChannelUpstreamHandler,ExternalResourceReleasable
@Sharable public class ExecutionHandler extends java.lang.Object implements ChannelUpstreamHandler, ChannelDownstreamHandler, ExternalResourceReleasable
Forwards an upstreamChannelEventto anExecutor.ExecutionHandleris often used when yourChannelHandlerperforms 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 anExecutionHandlerwill 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
ExecutionHandleris coupled with anOrderedMemoryAwareThreadPoolExecutorbecause it guarantees the correct event execution order and prevents anOutOfMemoryErrorunder load:public class DatabaseGatewayPipelineFactory implements
Please 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(); }OrderedMemoryAwareThreadPoolExecutorfor 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 oneExecutionHandlerto the pipeline.Using other
Although it's recommended to useExecutorimplementationOrderedMemoryAwareThreadPoolExecutor, you can use otherExecutorimplementations. However, you must note that otherExecutorimplementation 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 avoidOutOfMemoryError.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.Executorexecutorprivate booleanhandleDownstreamprivate booleanhandleUpstream
-
Constructor Summary
Constructors Constructor Description ExecutionHandler(java.util.concurrent.Executor executor)Creates a new instance with the specifiedExecutor.ExecutionHandler(java.util.concurrent.Executor executor, boolean handleDownstream, boolean handleUpstream)Creates a new instance with the specifiedExecutor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.ExecutorgetExecutor()Returns theExecutorwhich was specified with the constructor.voidhandleDownstream(ChannelHandlerContext ctx, ChannelEvent e)Handles the specified downstream event.protected booleanhandleReadSuspend(ChannelHandlerContext ctx, ChannelEvent e)Handle suspended readsvoidhandleUpstream(ChannelHandlerContext context, ChannelEvent e)Handles the specified upstream event.voidreleaseExternalResources()Shuts down theExecutorwhich was specified with the constructor and wait for its termination.
-
-
-
Constructor Detail
-
ExecutionHandler
public ExecutionHandler(java.util.concurrent.Executor executor)
Creates a new instance with the specifiedExecutor. Specify anOrderedMemoryAwareThreadPoolExecutorif unsure.
-
ExecutionHandler
public ExecutionHandler(java.util.concurrent.Executor executor, boolean handleDownstream, boolean handleUpstream)Creates a new instance with the specifiedExecutor. Specify anOrderedMemoryAwareThreadPoolExecutorif unsure.
-
-
Method Detail
-
getExecutor
public java.util.concurrent.Executor 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
public void handleUpstream(ChannelHandlerContext context, ChannelEvent e) throws java.lang.Exception
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:
java.lang.Exception
-
handleDownstream
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
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:
java.lang.Exception
-
handleReadSuspend
protected boolean handleReadSuspend(ChannelHandlerContext ctx, ChannelEvent e)
Handle suspended reads
-
-