diff --git a/api/src/main/java/com/sv/netty/netty/ServerHandler.java b/api/src/main/java/com/sv/netty/netty/ServerHandler.java index 9d189f8..61dd3d1 100644 --- a/api/src/main/java/com/sv/netty/netty/ServerHandler.java +++ b/api/src/main/java/com/sv/netty/netty/ServerHandler.java @@ -127,15 +127,15 @@ public class ServerHandler extends SimpleChannelInboundHandler { */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - logger.error("ServerHandler exceptionCaught",cause); Channel channel = ctx.channel(); - ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get(); -// Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId(); -// String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName(); -// DeviceType deviceType = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceType(); -// messageService.Offline(deviceName,venueId,deviceType); - if(channel.isActive() && param != null && param.getVenueId() != null) { - // 错误产生,关闭连接 + logger.error("{} - ServerHandler exceptionCaught",channel.hashCode(),cause); + if(channel.isActive()) { + ChannelParam param = channel.attr(NettyConstant.CHANNEL_PARAM).get(); + // 无论是合法连接还是非法扫描发来的乱码,发生错误立马掐断连接,绝不能手软 + if(channel.isActive() && param != null && param.getVenueId() != null) { + messageService.Offline(param.getDeviceName(),param.getVenueId()); + logger.info("{} - {} ServerHandler exceptionCaught,{}",channel.hashCode(),param.getDeviceName(),param.getVenueId(),cause); + } ctx.close(); } } @@ -149,19 +149,22 @@ public class ServerHandler extends SimpleChannelInboundHandler { if (evt instanceof IdleStateEvent){ IdleState state = ((IdleStateEvent) evt).state(); if (state == IdleState.READER_IDLE){ - logger.info("IdleStateEvent READER_IDLE 超时"); - ChannelParam param = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get(); - if (param != null && param.getVenueId() != null) { - // 原有逻辑 - Integer venueId = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getVenueId(); - String deviceName = ctx.channel().attr(NettyConstant.CHANNEL_PARAM).get().getDeviceName(); - messageService.Offline(deviceName,venueId); - ctx.channel().close(); + logger.info("IdleStateEvent READER_IDLE 超时,主动断开连接"); + Channel channel = ctx.channel(); + ChannelParam param = channel.attr(NettyConstant.CHANNEL_PARAM).get(); + if(channel.isActive() && param != null && param.getVenueId() != null) { + logger.info("{} IdleStateEvent READER_IDLE 超时,{}主动断开连接",param.getDeviceName(),param.getVenueId()); } + // 无论是否是有效设备,只要读超时(120秒未收到任何数据)就应该关闭连接。 + // 关闭连接会触发 channelInactive,在 channelInactive 中会根据 venueId 判断是否需要处理离线逻辑。 + channel.close(); } } + + // 由于每次 Idle 都会打日志显得冗余,这里只在有必要时看 Set connections = messageService.countConnection(); - logger.info("count connected device ! the count is " + connections.size() + " and they are + [" + connections.toString() + "]" ); + // 如果想要干净的日志,这个连接池统计其实可以去掉,或者缩小为 DEBUG + logger.info("count connected device ! the count is " + connections.size() + " and they are + [" + connections.toString() + "]" ); } }