netty 当数据量很多的时候会频繁触发channelInactivice是为什么,在线等待
如果在没有发送数据 只是连接请求可以看到连接数在3w多
window环境下运行,我的代码如下:
private void start() {
logHelper.info("Start server.....");
System.out.println("Start server.....");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
try {
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
//final String port = ResourceUtil.getKey("local_tcp_port");
//final String webPort = ResourceUtil.getKey("local_web_port");
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) {
try {
//ByteBuf delimiter = Unpooled.copiedBuffer("".getBytes());
//ByteBuf bb = Unpooled.buffer(0, 2048);
channel.config().setRecvByteBufAllocator(new AdaptiveRecvByteBufAllocator(4, 2048, Integer.MAX_VALUE));
ChannelPipeline pipeline = channel.pipeline();
//IdleStateHandler心跳机制,如果超时触发Handle中userEventTrigger()方法
pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.MINUTES));//5分钟心跳
//pipeline.addLast(new CustomDecoder(2048,0,0,0,0,false));
pipeline.addLast(new MyDecoder(2048));
// //过滤编码
// pipeline.addLast("decoder", new ByteArrayDecoder());
// //过滤编码
// pipeline.addLast("encoder", new ByteArrayEncoder());
InetSocketAddress inet = (InetSocketAddress)channel.localAddress();
if(inet.getPort()== Integer.parseInt(SMSTool.local_tcp_port))
pipeline.addLast(new ServerHandler());
// else if(inet.getPort() == Integer.parseInt(SMSTool.local_web_port))
// pipeline.addLast(new WebServerHandler(initCallQueue));
} catch (Exception e) {
// TODO: handle exception
//LogHelper.getIntance("dsd").equals(e.toString());
logHelper.error(e.toString());
}
}
}).option(ChannelOption.SO_BACKLOG, 1024)
//.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(65535))
.childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,并且返回ChannelFuture对象
ChannelFuture future = bootstrap.bind(Integer.parseInt(SMSTool.local_tcp_port)).sync();
ChannelFuture future2 = bootstrap.bind(Integer.parseInt(SMSTool.local_web_port)).sync();
System.out.println("server started!");
logHelper.info("server started!");
getConnectionTotal();
// 等待客户端的关闭
//Channel channel =
future.channel().closeFuture().sync();
future2.channel().closeFuture().sync();
ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);//内存泄漏检测 开发推荐PARANOID 线上SIMPLE
// 要记得是closeFuture(),作用是等待服务端的关闭
//channel.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully().syncUninterruptibly();
workerGroup.shutdownGracefully().syncUninterruptibly();
}
}
编译器代码:
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
try {
int readable = buffer.readableBytes();
if(readable > MAX_FRAME_SIZE) { // 缓冲区数据过大
buffer.skipBytes(readable); // 忽略所有可读的字节
// 抛出异常通知这个帧数据超长
throw new TooLongFrameException("帧数据超长");
}
//创建字节数组,buffer.readableBytes可读字节长度
byte[] b = new byte[readable];
//复制内容到字节数组b
buffer.readBytes(b);
out.add(buffer);
} catch (Exception e) {
// TODO: handle exception
LogHelper.getIntance().error("decode=>"+e.toString());
}
}
业务处理类:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// TODO Auto-generated method stub
ByteBuf buf = (ByteBuf)msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
byte[]req2 = new byte[req.length+2];
System.arraycopy(req, 0, req2, 0, req.length);
byte[] bb = Convert.shortToByte(SMSTool.GT06_FOOTER);
req2[req2.length-2] = bb[0];
req2[req2.length-1] = bb[1];
super.channelRead(ctx, req2);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
try {
ctx.channel().close();
//super.channelInactive(ctx);
//total.getList().remove(ctx.channel().remoteAddress().toString());
total.getConnection().decrementAndGet();//记录连接数
System.out.println("disconnect->"+ctx.channel().remoteAddress().toString());//断开连接
} catch (Exception e) {
// TODO: handle exception
logHelper.error(e.toString());
}
}
这个是服务端的主要代码,请高手指教!!!!!!