Netty开源库的初步使用

之前用Java socket实现模拟remote客户端进行并发登录,但断线重连、连接超时、网络闪断、网络拥塞和读写超时等实现较为麻烦。现学习使用netty开源库,在netty开源库基础上实现业务功能。
netty是JBOSS针对网络开发的一套应用框架,也是在NIO的基础上发展起来的。netty基于异步的事件驱动,具有高性能、高扩展性等特性,它提供了统一的底层协议接口,使得开发者从底层的网络协议(如TCP/IP、UDP)解放。对于使用者来说,只需要参考netty提供的若干例子和指南文档,就可开发基于netty的程序。
在开源论坛中下载netty的源码http://code.csdn.net/openkb/p-netty
先贴上一段代码:
服务端:

public void testServer() {
        //初始化channel的辅助类,为具体子类提供公共数据结构
        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));
        bootstrap.setOption("reuseAddress", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.soLinger", 2);
        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;
            }
        });
        //创建服务器端channel的辅助类,接收connection请求
        bootstrap.bind(new InetSocketAddress(8080));
    }

1.初始化ServerBootstrap,并注入NioServerSocketChannelFactory,用于创建通道(channel)等。从这里可以看出是通过Nio的方式来处理的,factory中放入两个线程池,主要用于接收connect和message

ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

2.为通道以及子通道(带前缀”child.”)设置相关的属性(socket属性)

        bootstrap.setOption("reuseAddress", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.soLinger", 2);

3.ChannelPipline中添加处理器(ChannelHandler),用于处理连接和消息

pipeline.addLast("decoder", new StringDecoder());
                pipeline.addLast("encoder", new StringEncoder());
                pipeline.addLast("handler", new HelloWorldServerHandler());

客户端:

public void testClient() {
    //创建客户端channel的辅助类,发起connection请求 
    ClientBootstrap bootstrap = new ClientBootstrap(
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));
    //It means one same HelloWorldClientHandler instance is going to handle multiple Channels and consequently the data will be corrupted.
    //基于上面这个描述,必须用到ChannelPipelineFactory每次创建一个pipeline
    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;
        }
    });
    //创建无连接传输channel的辅助类(UDP),包括client和server
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(
            "localhost", 8080));
    future.getChannel().getCloseFuture().awaitUninterruptibly();
    bootstrap.releaseExternalResources();
}

为了实现断线重连、连接超时、网络闪断、网络拥塞和读写超时等业务功能,在开源论坛上找到一些开源demo,protocol、echo、time与heartbeat等Demo。

其中protocol:
Netty开源库的初步使用_第1张图片

netty为protocol提供了编码器ProtocolEncoder;解码器ProtocolDecoder、ProtocolDecoderDeprecation;消息MsgType、ProtocolMsg;客户端ClientTask、ProtocolClient、ProtocolClientHandler;服务端ProtocolServer、ProtocolServerHandler

使用Demo构建所需要的业务模块,并进行完善和优化处理

http://blog.csdn.net/fengsser/article/details/8451000
http://blog.csdn.net/kobejayandy/article/details/11493717

你可能感兴趣的:(#,java,#,网络协议,jboss,开源)