当前位置:编程学习 > JAVA >>

netty future

本人新编写了一个netty的hello程序,代码如下:
public class HelloWorldServer {
    public static void main(String[]args){
        // Server服务启动器
        ServerBootstrap bootstrap =
                new ServerBootstrap(
                        new NioServerSocketChannelFactory(
                            Executors.newCachedThreadPool(),
                            Executors.newCachedThreadPool())
        );
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("decoder", new StringDecoder());
                pipeline.addLast("encoder", new StringEncoder());
                pipeline.addLast("handler", new HelloWorldServerHandler());
                return pipeline;
            }
        });
        bootstrap.bind(new InetSocketAddress(8088));
    }
}

public class HelloWorldServerHandler extends SimpleChannelHandler{
    public void channelConnected(ChannelHandlerContext ctx,ChannelStateEvent e)
            throws Exception {
        e.getChannel().write("Hello, World");
    }
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        System.out.println("Unexpected exception from downstream." + e.getCause());
        e.getChannel().close();
    }
}

public class HelloWorldClient {
    public static void main(String []args){
        ClientBootstrap bootstrap = new ClientBootstrap(
                new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool())
        );
        bootstrap.setPipelineFactory(
                new ChannelPipelineFactory(){
                    public ChannelPipeline getPipeline() {
                        ChannelPipeline pipeline = Channels.pipeline();
                        pipeline.addLast("decoder", new StringDecoder());
                        pipeline.addLast("encoder", new StringEncoder());
                        pipeline.addLast("handler", new HelloWorldClientHandler());
                        return pipeline;
                    }
                }
        );
        ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 8088));
        future.getChannel().getCloseFuture().awaitUninterruptibly();
        bootstrap.releaseExternalResources();
    }

}

public class HelloWorldClientHandler extends SimpleChannelHandler {
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        String message = (String) e.getMessage();
        System.out.println(message);
        e.getChannel().close();
    }
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
       System.out.println("Unexpected exception from downstream." + e.getCause());
        e.getChannel().close();
    }
}
在例子中ChannelFuture和java本身的Future有什么区别呀,另外可否有高手简单介绍一下nio netty和mina的区别,nio需要注册读和写事件,mina也是时间过滤的 netty采用什么机理呢
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,