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为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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sysu_lluozh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值