|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jboss.netty.handler.execution.ExecutionHandler
@ChannelHandler.Sharable public class ExecutionHandler
Forwards an upstream 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.
ExecutionHandler
to the pipeline.
Executor
implementationOrderedMemoryAwareThreadPoolExecutor
,
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 |
Constructor Summary | |
---|---|
ExecutionHandler(Executor executor)
Creates a new instance with the specified Executor . |
Method Summary | |
---|---|
Executor |
getExecutor()
Returns the Executor which was specified with the constructor. |
void |
handleDownstream(ChannelHandlerContext ctx,
ChannelEvent e)
Handles the specified downstream event. |
void |
handleUpstream(ChannelHandlerContext context,
ChannelEvent e)
Handles the specified upstream event. |
void |
releaseExternalResources()
Shuts down the Executor which was specified with the constructor
and wait for its termination. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ExecutionHandler(Executor executor)
Executor
.
Specify an OrderedMemoryAwareThreadPoolExecutor
if unsure.
Method Detail |
---|
public Executor getExecutor()
Executor
which was specified with the constructor.
public void releaseExternalResources()
Executor
which was specified with the constructor
and wait for its termination.
releaseExternalResources
in interface ExternalResourceReleasable
public void handleUpstream(ChannelHandlerContext context, ChannelEvent e) throws Exception
ChannelUpstreamHandler
handleUpstream
in interface ChannelUpstreamHandler
context
- the context object for this handlere
- the upstream event to process or intercept
Exception
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception
ChannelDownstreamHandler
handleDownstream
in interface ChannelDownstreamHandler
ctx
- the context object for this handlere
- the downstream event to process or intercept
Exception
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |