Netty开源库的初步使用
来源:互联网 发布:tumblr类似软件 编辑:程序博客网 时间:2024/05/17 07:20
之前用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为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
- Netty开源库的初步使用
- Netty使用初步
- netty初步使用
- netty初步使用2
- Netty使用初步
- Netty使用初步
- Netty使用初步
- Netty使用初步
- Netty使用初步(转)
- netty初步
- Netty 初步
- netty初步,与各个版本的比较
- Netty的使用模式
- netty的使用记录
- netty的使用
- netty的使用
- Netty的LengthFieldBasedFrameDecoder使用
- Netty 之 Netty使用Google的ProtoBuf
- Arcgis Engine中IVector3D关于旋转的注意事项
- java基础学习总结——流
- 赋值操作符
- 一个简单的递归实现数组组元素的组合
- iOS 推送问题 registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
- Netty开源库的初步使用
- android存储方式(内部存储、外部存储)
- C语言队列
- jquery datatables 的 配置参数
- RxJava的初学随笔(一)
- Set(HashSet、TreeSet)、泛型
- java自带线程池和队列详细讲解
- fatal: Not a git repository (or any of the parent directories): .git
- stm32库函数学习篇---NVIC与外部中断