`
san_yun
  • 浏览: 2676137 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

netty ExecutionHandler

 
阅读更多

前面(http://san-yun.iteye.com/blog/1693598)提到netty通过ChannelFuture来提供异步IO,但我实际测试中发现并不是总是这样的,需要ChannelPipelineFactory中指定ExecutionHandler。测试代码:

 

@ChannelPipelineCoverage("all")
public class DiscardServerHandler extends SimpleChannelHandler {

	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		Channel channel =e.getChannel();
		ChannelFuture future =  channel.write(e.getMessage());
		future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Thread.sleep(1000);  
                System.out.println(Thread.currentThread().getName());
                System.out.println("ok1");
            }
        });
		System.out.println(Thread.currentThread().getName());
		System.out.println("ok2");
	}
 

 

package com.duitang.test.test;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;


public class NettyTest {

    public static void main(String[] args) throws SecurityException, Exception {
        
        ChannelFactory factory =  
                new NioServerSocketChannelFactory (  
                        Executors.newCachedThreadPool(),  
                        Executors.newCachedThreadPool());  
        
        ServerBootstrap bootstrap = new ServerBootstrap (factory);
        DiscardServerHandler handler = new DiscardServerHandler();
        ChannelPipeline pipeline = bootstrap.getPipeline();  
        pipeline.addLast("handler", handler);  
        bootstrap.setOption("child.tcpNoDelay", true);  
        bootstrap.setOption("child.keepAlive", true);  
        bootstrap.bind(new InetSocketAddress(8080));
        
    }

}
 

输出ok1,ok2,同步执行。

 

public static void main(String[] args) {
		ChannelFactory factory = new NioServerSocketChannelFactory(
				Executors.newCachedThreadPool(),
				Executors.newCachedThreadPool());
		ServerBootstrap bootstrap = new ServerBootstrap(factory);
		ExecutionHandler executionHandler = new ExecutionHandler(
				new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576));
		DatabaseGatewayPipelineFactory gatewayPipelineFactory = new DatabaseGatewayPipelineFactory(
				executionHandler);
		bootstrap.setPipelineFactory(gatewayPipelineFactory);
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);
		bootstrap.bind(new InetSocketAddress(8080));
	}

	static class DatabaseGatewayPipelineFactory implements
			ChannelPipelineFactory {

		private final ExecutionHandler executionHandler;

		public DatabaseGatewayPipelineFactory(ExecutionHandler executionHandler) {
			this.executionHandler = executionHandler;
		}

		public ChannelPipeline getPipeline() throws Exception {
			ChannelPipeline pipeline = Channels.pipeline();
			pipeline.addFirst("decoder", new DiscardServerHandler());
			pipeline.addFirst("encoder", new DiscardServerHandler());
			pipeline.addFirst("executor", executionHandler);
			pipeline.addLast("handler", new DiscardServerHandler());
			return pipeline;
		}

	}

 输出

pool-3-thread-2
ok2
New I/O server worker #1-1
ok1

 

 

 

分享到:
评论

相关推荐

    Netty实现原理浅析.pdf

    此外,Netty还提供了一些特殊的`ChannelHandler`实现,例如`ExecutionHandler`,它可以在处理链中添加线程池,以支持异步处理模型。这样做的好处是可以有效避免长时间运行的任务阻塞事件循环线程,提高系统的并发...

    Netty实现原理浅析.docx

    为了解决这个问题,Netty 提供了ExecutionHandler,它可以在ChannelPipeline中加入线程池,将耗时操作异步化。Netty 提供了两种线程池实现:MemoryAwareThreadPoolExecutor和OrderedMemoryAwareThreadPoolExecutor。...

    Netty实现原理浅析

    例如,如果业务处理的handler耗时较长,Netty允许开发者通过添加ExecutionHandler来实现handler的线程池化,从而在不修改原有业务逻辑的情况下提升并发处理能力。Netty提供了MemoryAwareThreadPoolExecutor和...

    netty4.0中的新变化和注意点鸟窝.pdf

    - 线程模型的改进,执行器 `ExecutionHandler` 的使用更加明确。 - `Codec` 框架的变化,包括 `EmbeddedChannel` 的实现,简化了编码和解码的嵌入。 6. **其他改动**: - `ChannelOption` 和 `AttributeMap` 的...

    java nio 原理浅析

    如果业务处理耗时较长,可以考虑引入ExecutionHandler实现线程池化,以提升并发性能。Netty提供了MemoryAwareThreadPoolExecutor和OrderedMemoryAwareThreadPoolExecutor两种线程池选项,前者限制待处理任务的数量,...

Global site tag (gtag.js) - Google Analytics