netty 当数据量很多的时候会频繁触发channelInactivice是为什么,在线等待

tt6550619 tt6550619 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如果在没有发送数据 只是连接请求可以看到连接数在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());
}

}
这个是服务端的主要代码,请高手指教!!!!!!
...全文

相关推荐

fengmian123

fengmian123

CiYuanShuos

QYR-18900-2026-2032全球与中国智能旋转导向钻井系统市场现状及未来发展趋势 Sample-shijunzhong.docx

QYR-18900-2026-2032全球与中国智能旋转导向钻井系统市场现状及未来发展趋势 Sample-shijunzhong.docx

qyresearch报告

XERJ 是一款受到社区信赖的本地 AI 搜索引擎

XERJ 是一款受到社区信赖的本地 AI 搜索引擎,能自动为任意文件夹建立索引,让编程智能体直接检索所需代码,省去逐个读取文件消耗的 token。它的主要用途是参考现有实现来写代码(reference coding):接到任务后,智能体下载与任务相关的开源仓库,建立索引,再查阅这些项目的解决方法。 在一项对照研究中,这种做法让智能体的输出 token 用量降至原来的约 1/2.7,同时保持 16/16 的任务完成率(案例研究)。用户在日常开发中报告的用量约为原来的 1/5(使用反馈)。有了可检索的参考实现,较小、较便宜的模型也可能胜过只凭记忆工作的昂贵模型,减少反复修改的次数。把下面这段提示词交给智能体即可开始

yiersansiwu123d

DocRes.Vulkan.WinForms.VS2022-Source-r1.1-20261016-102846-206.zip

DocRes.Vulkan.WinForms.VS2022-Source-r1.1-20261016-102846-206.zip 博客地址:https://blog.csdn.net/lw112190/article/details/168119884

天天代码码天天

电力系统连锁故障一种用于识别引发连锁故障的多重故障集合的“随机化学”算法研究(Matlab代码实现)

内容概要:本文研究了一种基于“随机化学”算法的多重故障集合识别方法,旨在准确识别电力系统中可能引发连锁故障的关键故障组合。该方法结合ADMM(交替方向乘子法)优化框架,建立了考虑连锁故障传播路径的N-k多阶段双层优化模型,并设计了高效的故障场景筛选机制,以应对大规模电网中连锁故障风险分析的高复杂性挑战。通过Matlab代码实现了算法的全流程仿真验证,结果表明该方法在识别高风险故障集方面具有较高的有效性与计算效率,为电力系统的安全评估、脆弱性分析及预防控制策略制定提供了先进的技术支撑。; 适合人群:具备电力系统分析、优化理论基础及Matlab编程能力,从事电力系统安全稳定、连锁故障防御、风险评估等相关研究的科研人员与工程技术人员。; 使用场景及目标:①应用于大规模电力系统中高风险N-k故障场景的快速识别与排序;②支撑基于N-k安全准则的电网脆弱环节分析与主动防御策略设计;③为运行调度、规划检修及应急管理提供科学的故障预案筛选工具。; 阅读建议:建议读者结合Matlab代码与论文中的模型推导,深入理解随机化学算法在双层优化框架中的实现逻辑,重点关注ADMM的分解协调机制与故障传播路径的建模方法,并通过复现仿真案例掌握其在实际电网中的应用流程与参数设置技巧。

荧光Matlab
评论 2
收藏 已收藏
分享
返回顶部
tt6550619
Java SE 62,620 成员 307,251 内容