netty 当数据量很多的时候会频繁触发channelInactivice是为什么,在线等待
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());
}
}
这个是服务端的主要代码,请高手指教!!!!!!
相关推荐
科技中介服务机构在评估技术成果价值时,如何避免主观判断带来的信息偏差?.docx
科技中介服务机构在评估技术成果价值时,如何避免主观判断带来的信息偏差?
高校技术转移办公室人员如何利用知识图谱实现技术成果价值挖掘?.docx
科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。
课程课qdqwdwdqwdqw程课程课程课程课程课程课程课程课程课程课程课程课程
课程课qdqwdwdqwdqw程课程课程课程课程课程课程课程课程课程课程课程课程
国央企在推动自主创新与技术转移时,如何提升技术供需对接的效率与成功率?.docx
科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。
阻尼器磁流变阻尼器MR Damper非线性(阻尼、滞回)特性研究(Matlab代码实现)
内容概要:本文系统研究了磁流变阻尼器(MR Damper)的非线性力学特性,重点聚焦其阻尼力响应与典型滞回行为,并基于Matlab提供了完整的仿真建模与实现代码。通过构建MR阻尼器的动力学模型,深入分析其在外加磁场调控下的非线性、强滞回的力-位移与力-速度关系,利用数值仿真手段复现了其复杂的动态响应特征。研究涵盖了从模型建立、关键参数设定、外部激励信号设计到仿真结果可视化分析的全过程,旨在揭示该智能材料器件在振动控制应用中的内在机理与性能边界。该仿真平台对于理解半主动悬架、结构隔震等领域的核心元件行为具有重要参考价值。; 适合人群:具备一定Matlab编程基础和动力学系统基础知识,从事机械工程、车辆工程、土木工程结构振动控制、智能材料与结构或机电一体化等方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握磁流变阻尼器非线性滞回特性的数学建模与计算机仿真方法;②深入理解其力-位移/力-速度曲线的形成机制及磁场对阻尼力的调控规律;③为后续设计半主动控制系统、进行控制器开发或开展相关实验验证提供可靠的仿真基础与理论支撑; 阅读建议:建议读者结合Matlab代码进行逐行调试与参数扫描,通过改变输入激励(如正弦、复合频率位移)和模型参数(如磁场强度、粘滞系数),直观观察其对滞回环形状、峰值阻尼力和能量耗散的影响,以深化对非线性动态行为的理解,同时推荐查阅相关实验文献以对比验证仿真结果的准确性。


Java SE
