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 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 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 Executor
private final boolean
private 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 theExecutor
which was specified with the constructor.void
Handles the specified downstream event.protected boolean
Handle suspended readsvoid
handleUpstream
(ChannelHandlerContext context, ChannelEvent e) Handles the specified upstream event.void
Shuts down theExecutor
which 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 anOrderedMemoryAwareThreadPoolExecutor
if unsure. -
ExecutionHandler
Creates a new instance with the specifiedExecutor
. Specify anOrderedMemoryAwareThreadPoolExecutor
if unsure.
-
-
Method Details
-
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
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:
Exception
-
handleDownstream
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:
Exception
-
handleReadSuspend
Handle suspended reads
-