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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值