Commit 548b4514 by giaogiao

优化WsReadHandler

parent fab8dc3d
...@@ -46,9 +46,9 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram ...@@ -46,9 +46,9 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
* io密集型任务配置尽可能多的线程数量 * io密集型任务配置尽可能多的线程数量
*/ */
private final static ExecutorService TASK_THREAD_POOL_EXECUTOR = private final static ExecutorService TASK_THREAD_POOL_EXECUTOR =
new ThreadPoolExecutor(WsConstants.CPU_PROCESSORS * 5, WsConstants.CPU_PROCESSORS * 50, new ThreadPoolExecutor(WsConstants.CPU_PROCESSORS * 5, WsConstants.CPU_PROCESSORS * 10,
3L, TimeUnit.MILLISECONDS, 10L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1), NAMED_THREAD_FACTORY, new ThreadPoolExecutor.CallerRunsPolicy()); new LinkedBlockingQueue<Runnable>(10), NAMED_THREAD_FACTORY, new ThreadPoolExecutor.CallerRunsPolicy());
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) { protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) {
...@@ -92,7 +92,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram ...@@ -92,7 +92,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
readWsData.convertModel(data, ctx, appKey, clientId); readWsData.convertModel(data, ctx, appKey, clientId);
} catch (Exception e) { } catch (Exception e) {
log.error("系统繁忙data:" + data + ",appKey:" + appKey + ",clientId:" + clientId + log.error("系统繁忙data:" + data + ",appKey:" + appKey + ",clientId:" + clientId +
",channelId:" + ctx.channel().id().asShortText(), e); ",channelId:" + ctx.channel().id().asLongText(), e);
} }
} }
...@@ -126,9 +126,8 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram ...@@ -126,9 +126,8 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
break; break;
} }
log.info(clientId + "超时事件:" + eventType); log.info(clientId + "超时事件:" + eventType);
if (readIdleTimes > 5) { if (readIdleTimes >= 5) {
log.info(clientId + ". [server]读空闲超过3次,关闭连接"); log.info(clientId + ".读空闲超过5次关闭连接");
// ctx.channel().writeAndFlush("you are out");
ctx.channel().close(); ctx.channel().close();
} }
} }
...@@ -144,13 +143,13 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram ...@@ -144,13 +143,13 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
// @Override // @Override
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// String userIdByChannel = mangerChannelService.getInfoByChannel(ctx); // String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
// log.info("uid:" + userIdByChannel + ",ws异常,channelId:" + ctx.channel().id().asShortText(), cause); // log.info("uid:" + userIdByChannel + ",ws异常,channelId:" + ctx.channel().id().asLongText(), cause);
// } // }
@Override @Override
public void handlerAdded(ChannelHandlerContext ctx) { public void handlerAdded(ChannelHandlerContext ctx) {
String userIdByChannel = mangerChannelService.getInfoByChannel(ctx); String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
log.info("连接WS成功handlerAdded,uid:" + userIdByChannel + "," + ",channelId:" + ctx.channel().id().asShortText()); log.info("连接WS成功handlerAdded,uid:" + userIdByChannel + "," + ",channelId:" + ctx.channel().id().asLongText());
} }
...@@ -163,7 +162,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram ...@@ -163,7 +162,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
// @Override // @Override
// public void channelInactive(ChannelHandlerContext ctx) { // public void channelInactive(ChannelHandlerContext ctx) {
// String userIdByChannel = mangerChannelService.getInfoByChannel(ctx); // String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
// log.info("uid:" + userIdByChannel + "," + "channelInactive" + ",channelId:" + ctx.channel().id().asShortText()); // log.info("uid:" + userIdByChannel + "," + "channelInactive" + ",channelId:" + ctx.channel().id().asLongText());
// } // }
/** /**
...@@ -172,7 +171,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram ...@@ -172,7 +171,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
@Override @Override
public void handlerRemoved(ChannelHandlerContext ctx) { public void handlerRemoved(ChannelHandlerContext ctx) {
String userIdByChannel = mangerChannelService.getInfoByChannel(ctx); String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
log.info("uid:" + userIdByChannel + "," + "handlerRemoved" + ",channelId:" + ctx.channel().id()); log.info("uid:" + userIdByChannel + "," + "handlerRemoved" + ",channelId:" + ctx.channel().id().asLongText());
// 关掉连接 // 关掉连接
ctx.close(); ctx.close();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment