@ChannelHandler.Sharable public class ExecutionHandler extends java.lang.Object implements ChannelUpstreamHandler, ChannelDownstreamHandler, ExternalResourceReleasable
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.
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.ChannelHandler.Sharable| 构造器和说明 |
|---|
ExecutionHandler(java.util.concurrent.Executor executor)
Creates a new instance with the specified
Executor. |
ExecutionHandler(java.util.concurrent.Executor executor,
boolean handleDownstream,
boolean handleUpstream)
Creates a new instance with the specified
Executor. |
| 限定符和类型 | 方法和说明 |
|---|---|
java.util.concurrent.Executor |
getExecutor()
Returns the
Executor 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 reads
|
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. |
public ExecutionHandler(java.util.concurrent.Executor executor)
Executor.
Specify an OrderedMemoryAwareThreadPoolExecutor if unsure.public ExecutionHandler(java.util.concurrent.Executor executor,
boolean handleDownstream,
boolean handleUpstream)
Executor.
Specify an OrderedMemoryAwareThreadPoolExecutor if unsure.public java.util.concurrent.Executor getExecutor()
Executor which was specified with the constructor.public void releaseExternalResources()
Executor which was specified with the constructor
and wait for its termination.public void handleUpstream(ChannelHandlerContext context, ChannelEvent e) throws java.lang.Exception
ChannelUpstreamHandlerhandleUpstream 在接口中 ChannelUpstreamHandlercontext - the context object for this handlere - the upstream event to process or interceptjava.lang.Exceptionpublic void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
ChannelDownstreamHandlerhandleDownstream 在接口中 ChannelDownstreamHandlerctx - the context object for this handlere - the downstream event to process or interceptjava.lang.Exceptionprotected boolean handleReadSuspend(ChannelHandlerContext ctx, ChannelEvent e)