Netty 是JBoss旗下的io传输的框架,他利用java里面的nio来实现高效,稳定的io传输。
作为io传输,就会有client和server,下面我们看看用netty怎样写client和server
Client:
需要做的事情:
1.配置client启动类
ClientBootstrap bootstrap = new ClientBootstrap(..)
2.根据不同的协议或者模式为client启动类设置pipelineFactory。
这里telnet pipline Factory 在netty中已经存在,所有直接用
bootstrap.setPipelineFactory(new TelnetClientPipelineFactory());
也可以自己定义
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
new DiscardClientHandler(firstMessageSize));
}
});
这里DiscardClientHandler 就是自己定义的handler,他需要
public class DiscardServerHandler extends SimpleChannelUpstreamHandler
继承SimpleChannelUpstreamHandler 来实现自己的handler。这里DiscardClientHandler
是处理自己的client端的channel,他的
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
// Server is supposed to send nothing. Therefore, do nothing.
}
可以看到Discard client不需要接受任何信息
3.连接server
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
这里解释一下channelFuture:
在Netty中所有的io操作都是异步的,这也就是意味任何io访问,那么就立即返回处理,并且不能确保
返回的数据全部完成。因此就出现了channelFuture,channelFuture在传输数据时候包括数据和状态两个
部分。他只有Uncompleted和Completed
+---------------------------+ | Completed successfully | +---------------------------+ +----> isDone() = true | +--------------------------+ | | isSuccess() = true | | Uncompleted | | +===========================+ +--------------------------+ | | Completed with failure | | isDone() = false | | +---------------------------+ | isSuccess() = false |----+----> isDone() = true | | isCancelled() = false | | | getCause() = non-null | | getCause() = null | | +===========================+ +--------------------------+ | | Completed by cancellation | | +---------------------------+ +----> isDone() = true | | isCancelled() = true | +---------------------------+
既然netty io是异步的,那么如何知道channel传送完成有两种方式,一种添加监听器
addListener(ChannelFutureListener) 还有一种直接调用await()方法,这两种方式
有下面的区别
监听器:是以事件模式的,因此代码就需要用事件模式的样式去写,相当复杂,但他是non-blocking模式的
性能方面要比await方法好,而且不会产生死锁情况
await(): 直接方法调用,使用简单,但是他是blocking模式,性能方面要弱而且会产生死锁情况
不要在ChannelHandler 里面调用await(),这是因为通常在channelHandler里的event method是被i/o线程调用的
(除非ChannelPipeline里面有个ExecutionHandler),那么如果这个时候用await就容易产生死锁。
// BAD - NEVER DO THIS@Override
public void messageReceived(ChannelHandlerContext
ctx,MessageEvent
e) { if (e.getMessage() instanceof GoodByeMessage) {ChannelFuture
future = e.getChannel().close(); future.awaitUninterruptibly(); // Perform post-closure operation // ... } } // GOOD@Override
public void messageReceived(ChannelHandlerContext
ctx,MessageEvent
e) { if (e.getMessage() instanceof GoodByeMessage) {ChannelFuture
future = e.getChannel().close(); future.addListener(newChannelFutureListener
() { public void operationComplete(ChannelFuture
future) { // Perform post-closure operation // ... } }); } }
虽然await调用比较危险,但是你确保不是在一个i/o 线程中调用该方法,毕竟await方法还是很简洁方便的,如果
调用该方法是在一个i/o 线程,那么就会抛出 IllegalStateException
await的timeout和i/o timeout区别
需要注意的是这两个timeout是不一样的, #await(long),#await(long, TimeUnit), #awaitUninterruptibly(long),
#awaitUninterruptibly(long, TimeUnit) 这里面的timeout也i/o timeout 没有任何关系,如果io timeout,那么
channelFuture 将被标记为completed with failure,而await的timeout 与future完全没有关系,只是await动作的
timeout。
// BAD - NEVER DO THISClientBootstrap
b = ...;ChannelFuture
f = b.connect(...); f.awaitUninterruptibly(10, TimeUnit.SECONDS); if (f.isCancelled()) { // Connection attempt cancelled by user } else if (!f.isSuccess()) { // You might get a NullPointerException here because the future // might not be completed yet. f.getCause().printStackTrace(); } else { // Connection established successfully } // GOODClientBootstrap
b = ...; // Configure the connect timeout option. b.setOption("connectTimeoutMillis", 10000);ChannelFuture
f = b.connect(...); f.awaitUninterruptibly(); // Now we are sure the future is completed. assert f.isDone(); if (f.isCancelled()) { // Connection attempt cancelled by user } else if (!f.isSuccess()) { f.getCause().printStackTrace(); } else { // Connection established successfully }
4.等待或监听数据全部完成
如: future.getChannel().getCloseFuture().awaitUninterruptibly();
5.释放连接等资源
bootstrap.releaseExternalResources();
Server:
1.配置server
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
2.设置pipeFactory
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new EchoServerHandler());
}
});
或者
bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
3.绑定sever端端口
bootstrap.bind(new InetSocketAddress(8080));
======================netty个人理解分割线======================================
netty是个非常好用的nio框架,提供了安全、快捷的tcp/ip、udp/ip通讯方式。网上也有好多文章,netty3.6的官方文档是这个http://netty.io/3.6/guide/,写得很好。
下面就读着这篇文档,做下笔记:
先看两段代码:
一:DiscardServer,这是个服务端的代码,是服务端的启动程序(创建channel,指定通道处理程序)
- public class DiscardServer {
- private final int port;
- public DiscardServer(int port) {
- this.port = port;
- }
- public void run() {
- // Configure the server.
- ServerBootstrap bootstrap = new ServerBootstrap(
- new NioServerSocketChannelFactory(
- Executors.newCachedThreadPool(),
- Executors.newCachedThreadPool()));
- // Set up the pipeline factory.
- bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
- public ChannelPipeline getPipeline() throws Exception {
- return Channels.pipeline(new DiscardServerHandler());
- }
- });
- // Bind and start to accept incoming connections.
- bootstrap.bind(new InetSocketAddress(port));
- }
- public static void main(String[] args) throws Exception {
- int port;
- if (args.length > 0) {
- port = Integer.parseInt(args[0]);
- } else {
- port = 8080;
- }
- new DiscardServer(port).run();
- }
- }
既然是通信,那当然有通道,Channel就是通道,通道是谁创建的呢,当然是ChannelFactory
代码中的NioServerSocketChannelFactory就是服务端的NIO的Socket的通道工厂,名字起得很直白吧。
创建这个工厂需要两个线程池——Executor(Executors.newCachedThreadPool()返回的ExecutorService,ExecutorService继承自Executor),这两个Executor一个用来执行boss线程,一个用来执行I/O工人线程。
工厂设置好之后, 就该创建Channel了,创建Channel是个复杂的过程,netty提供了一个帮助类ServerBootstrap,使用它可以便捷的执行这个创建过程。
有了通道后,当然会有相关的通讯处理,这些处理就是ChannelHandler(DiscardServerHandler就是自定义的handler,代码见二:DiscardServerHandler),ChannelHandler使用ChannelPipeline来设置(添加、移除、获取),ChannelPipelineFactory当然是用来创建ChannelPipeline的了。
最后,绑定个端口号,开始启动服务吧
二:DiscardServerHandler,通道处理程序
- public class DiscardServerHandler extends SimpleChannelUpstreamHandler {
- private static final Logger logger = Logger.getLogger(
- DiscardServerHandler.class.getName());
- private long transferredBytes;
- public long getTransferredBytes() {
- return transferredBytes;
- }
- @Override
- public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
- if (e instanceof ChannelStateEvent) {
- logger.info(e.toString());
- }
- // Let SimpleChannelHandler call actual event handler methods below.
- super.handleUpstream(ctx, e);
- }
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
- // Discard received data silently by doing nothing.
- transferredBytes += ((ChannelBuffer) e.getMessage()).readableBytes();
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
- // Close the connection when an exception is raised.
- logger.log(
- Level.WARNING,
- "Unexpected exception from downstream.",
- e.getCause());
- e.getChannel().close();
- }
- }
这个通道处理程序继承自SimpleChannelUpstreamHandler,最终实现了ChannelHandler,netty是基于事件驱动的,所以DiscardServerHandler实现的方法中都会有相关的事件
messageReceived:当消息到达时执行
exceptionCaught:当发生异常时执行
messageReceived有两个参数,一个是ChannelHandlerContext,一个是MessageEvent,messageEvent就是消息事件,从消息事件中可以取出消息(getMessage())
服务端的代码搞定了,下面上客户端的代码,客户端的代码跟服务端的大体一致:
一:DiscardClient
- public class DiscardClient {
- private final String host;
- private final int port;
- private final int firstMessageSize;
- public DiscardClient(String host, int port, int firstMessageSize) {
- this.host = host;
- this.port = port;
- this.firstMessageSize = firstMessageSize;
- }
- public void run() {
- // Configure the client.
- ClientBootstrap bootstrap = new ClientBootstrap(
- new NioClientSocketChannelFactory(
- Executors.newCachedThreadPool(),
- Executors.newCachedThreadPool()));
- // Set up the pipeline factory.
- bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
- public ChannelPipeline getPipeline() throws Exception {
- return Channels.pipeline(
- new DiscardClientHandler(firstMessageSize));
- }
- });
- // Start the connection attempt.
- ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
- // Wait until the connection is closed or the connection attempt fails.
- future.getChannel().getCloseFuture().awaitUninterruptibly();
- // Shut down thread pools to exit.
- bootstrap.releaseExternalResources();
- }
- public static void main(String[] args) throws Exception {
- // Print usage if no argument is specified.
- if (args.length < 2 || args.length > 3) {
- System.err.println(
- "Usage: " + DiscardClient.class.getSimpleName() +
- " <host> <port> [<first message size>]");
- return;
- }
- // Parse options.
- final String host = args[0];
- final int port = Integer.parseInt(args[1]);
- final int firstMessageSize;
- if (args.length == 3) {
- firstMessageSize = Integer.parseInt(args[2]);
- } else {
- firstMessageSize = 256;
- }
- new DiscardClient(host, port, firstMessageSize).run();
- }
- }
二:
- public class DiscardClientHandler extends SimpleChannelUpstreamHandler {
- private static final Logger logger = Logger.getLogger(
- DiscardClientHandler.class.getName());
- private long transferredBytes;
- private final byte[] content;
- public DiscardClientHandler(int messageSize) {
- if (messageSize <= 0) {
- throw new IllegalArgumentException(
- "messageSize: " + messageSize);
- }
- content = new byte[messageSize];
- }
- public long getTransferredBytes() {
- return transferredBytes;
- }
- @Override
- public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
- if (e instanceof ChannelStateEvent) {
- if (((ChannelStateEvent) e).getState() != ChannelState.INTEREST_OPS) {
- logger.info(e.toString());
- }
- }
- // Let SimpleChannelHandler call actual event handler methods below.
- super.handleUpstream(ctx, e);
- }
- @Override
- public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
- // Send the initial messages.
- generateTraffic(e);
- }
- @Override
- public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) {
- // Keep sending messages whenever the current socket buffer has room.
- generateTraffic(e);
- }
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
- // Server is supposed to send nothing. Therefore, do nothing.
- }
- @Override
- public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) {
- transferredBytes += e.getWrittenAmount();
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
- // Close the connection when an exception is raised.
- logger.log(
- Level.WARNING,
- "Unexpected exception from downstream.",
- e.getCause());
- e.getChannel().close();
- }
- private void generateTraffic(ChannelStateEvent e) {
- // Keep generating traffic until the channel is unwritable.
- // A channel becomes unwritable when its internal buffer is full.
- // If you keep writing messages ignoring this property,
- // you will end up with an OutOfMemoryError.
- Channel channel = e.getChannel();
- while (channel.isWritable()) {
- ChannelBuffer m = nextMessage();
- if (m == null) {
- break;
- }
- channel.write(m);
- }
- }
- private ChannelBuffer nextMessage() {
- return ChannelBuffers.wrappedBuffer(content);
- }
- }