Netty读写检测与长连接
使用《Netty多客户端通信》中的客户端代码进行测试,这里实现server端的代码:
MyServer.java
package com.dongqiang.netty.fourthexample;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
/**
* Netty读写检测,使用thirdexample中的Client测试
*/
public class MyServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO)).childHandler(new MyServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind("localhost", 8899).sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
这里添加了一个handler()方法,顾名思义,handler是设置在bossGroup的,childHandler()设置给workerGroup。这里打开了日志级别,为INFO。那么启动服务器的时候,会有日志打印出来。
MyServerInitializer.java
package com.dongqiang.netty.fourthexample;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.TimeUnit;
/**
* Created by dongqiang on 2017/6/11.
*/
public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new IdleStateHandler(5, 7, 10, TimeUnit.SECONDS));
pipeline.addLast(new MyServerHandler());
}
}
添加的第二个处理器,是空闲状态处理器,由Netty提供,第一个参数为readerIdleTime(读空闲时间),第二个参数为writerIdleTime(写空闲时间),第三个参数为allIdleTime(读写总空闲时间),第四个参数为unit时间单位。
自定义MyServerHandler处理器:
MyServerHandler.java
package com.dongqiang.netty.fourthexample;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
/**
* Created by dongqiang on 2017/6/11.
*/
public class MyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
IdleState state = event.state();
String eventType = "";
switch (state) {
case READER_IDLE:
eventType = "读空闲";
break;
case WRITER_IDLE:
eventType = "写空闲";
break;
case ALL_IDLE:
eventType = "读写空闲";
break;
}
System.out.println(ctx.channel().remoteAddress() + "超时事件:" + eventType);
ctx.channel().close();
}
}
}
使用《Netty多客户端通信》客户端代码,然后启动服务端和客户端,然后在客户端不断输入,可以看到写空闲,改小总时间,可以看到读写空闲。所谓读写,都是针对server程序而言。所谓空闲,就是没有读操作或者写操作。