import java.util.concurrent.Executor; import org.jboss.netty.channel.ChannelDownstreamHandler; import org.jboss.netty.channel.ChannelEvent; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.ChannelHandler.Sharable; import org.jboss.netty.handler.execution.ChannelEventRunnable; import org.jboss.netty.util.ExternalResourceReleasable; import org.jboss.netty.util.internal.ExecutorUtil; @Sharable public class DownStreamExecutionHandler implements ChannelUpstreamHandler, ChannelDownstreamHandler, ExternalResourceReleasable { private final Executor executor; /** * Creates a new instance with the specified {@link Executor}. */ public DownStreamExecutionHandler(Executor executor) { if (executor == null) { throw new NullPointerException("executor"); } this.executor = executor; } /** * Returns the {@link Executor} which was specified with the constructor. */ public Executor getExecutor() { return executor; } /** * Shuts down the {@link Executor} which was specified with the constructor * and wait for its termination. */ public void releaseExternalResources() { ExecutorUtil.terminate(getExecutor()); } public void handleUpstream(ChannelHandlerContext context, ChannelEvent e) throws Exception { context.sendUpstream(e); } public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { executor.execute(new ChannelEventRunnable(ctx, e)); } }