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 upstreamChannelEvent
to anExecutor
.ExecutionHandler
is often used when yourChannelHandler
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 anExecutionHandler
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 anOrderedMemoryAwareThreadPoolExecutor
because it guarantees the correct event execution order and prevents anOutOfMemoryError
under load:public class DatabaseGatewayPipelineFactory implements
Please refer toChannelPipelineFactory
{ private finalExecutionHandler
executionHandler; public DatabaseGatewayPipelineFactory(ExecutionHandler
executionHandler) { this.executionHandler = executionHandler; } publicChannelPipeline
getPipeline() { returnChannels
.pipeline( new DatabaseGatewayProtocolEncoder(), new DatabaseGatewayProtocolDecoder(), executionHandler, // Must be shared new DatabaseQueryingHandler()); } } ... public static void main(String[] args) {ServerBootstrap
bootstrap = ...; ...ExecutionHandler
executionHandler = 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
Although it's recommended to useExecutor
implementationOrderedMemoryAwareThreadPoolExecutor
, you can use otherExecutor
implementations. However, you must note that otherExecutor
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 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.Executor
executor
private boolean
handleDownstream
private boolean
handleUpstream
-
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.Executor
getExecutor()
Returns theExecutor
which was specified with the constructor.void
handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
Handles the specified downstream event.protected boolean
handleReadSuspend(ChannelHandlerContext ctx, ChannelEvent e)
Handle suspended readsvoid
handleUpstream(ChannelHandlerContext context, ChannelEvent e)
Handles the specified upstream event.void
releaseExternalResources()
Shuts down theExecutor
which 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 anOrderedMemoryAwareThreadPoolExecutor
if unsure.
-
ExecutionHandler
public ExecutionHandler(java.util.concurrent.Executor executor, boolean handleDownstream, boolean handleUpstream)
Creates a new instance with the specifiedExecutor
. Specify anOrderedMemoryAwareThreadPoolExecutor
if unsure.
-
-
Method Detail
-
getExecutor
public java.util.concurrent.Executor getExecutor()
Returns theExecutor
which was specified with the constructor.
-
releaseExternalResources
public void releaseExternalResources()
Shuts down theExecutor
which was specified with the constructor and wait for its termination.- Specified by:
releaseExternalResources
in interfaceExternalResourceReleasable
-
handleUpstream
public void handleUpstream(ChannelHandlerContext context, ChannelEvent e) throws java.lang.Exception
Description copied from interface:ChannelUpstreamHandler
Handles the specified upstream event.- Specified by:
handleUpstream
in 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:ChannelDownstreamHandler
Handles the specified downstream event.- Specified by:
handleDownstream
in 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
-
-