Netty开源库的初步使用
2016-01-18 21:05
344 查看
之前用Java socket实现模拟remote客户端进行并发登录,但断线重连、连接超时、网络闪断、网络拥塞和读写超时等实现较为麻烦。现学习使用netty开源库,在netty开源库基础上实现业务功能。
netty是JBOSS针对网络开发的一套应用框架,也是在NIO的基础上发展起来的。netty基于异步的事件驱动,具有高性能、高扩展性等特性,它提供了统一的底层协议接口,使得开发者从底层的网络协议(如TCP/IP、UDP)解放。对于使用者来说,只需要参考netty提供的若干例子和指南文档,就可开发基于netty的程序。
在开源论坛中下载netty的源码http://code.csdn.net/openkb/p-netty
先贴上一段代码:
服务端:
1.初始化ServerBootstrap,并注入NioServerSocketChannelFactory,用于创建通道(channel)等。从这里可以看出是通过Nio的方式来处理的,factory中放入两个线程池,主要用于接收connect和message
2.为通道以及子通道(带前缀”child.”)设置相关的属性(socket属性)
3.ChannelPipline中添加处理器(ChannelHandler),用于处理连接和消息
客户端:
为了实现断线重连、连接超时、网络闪断、网络拥塞和读写超时等业务功能,在开源论坛上找到一些开源demo,protocol、echo、time与heartbeat等Demo。
其中protocol:
netty为protocol提供了编码器ProtocolEncoder;解码器ProtocolDecoder、ProtocolDecoderDeprecation;消息MsgType、ProtocolMsg;客户端ClientTask、ProtocolClient、ProtocolClientHandler;服务端ProtocolServer、ProtocolServerHandler
使用Demo构建所需要的业务模块,并进行完善和优化处理
/article/9854522.html
http://blog.csdn.net/kobejayandy/article/details/11493717
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构建所需要的业务模块,并进行完善和优化处理
/article/9854522.html
http://blog.csdn.net/kobejayandy/article/details/11493717
相关文章推荐
- android存储方式(内部存储、外部存储)
- 一个简单的递归实现数组组元素的组合
- 赋值操作符
- 作品第三课----判断几位数字
- Android笔记--对缓存、三级缓存和Android的Dalvik虚拟机和java编译过程的理解
- java基础学习总结——流
- IP等级
- DIV层叠的非absolute(z-index)方法
- 机器学习(十):Apriori算法
- 自定义View-七日利率折线图
- java基础学习总结——线程(二)
- ORA-00600 [kcratr_nab_less_than_odr] ORA-01552 发生及解决方案
- hdu 1387 Team Queue (链表)
- 也说php从mysql数据库通过服务器端json返回数据出现乱码问题
- Python实现同时找最大最小值(优化版本)
- RHEL7.0 日志系统
- java基础学习总结——线程(一)
- 并查集-HDU-1232-畅通工程
- dubbo 服务端和客户端调用
- zend studio12.5破解方法