Netty Server引导程序:
EventLoopGroup bg = new NioEventLoopGroup(1, new CamelThreadFactory("Thread#", "NettyServerTCPBoss", false));
EventLoopGroup wg = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new CamelThreadFactory("Thread#", "NettyServerTCPWorker", false));
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
Netty管道配置:
ch.pipeline().addLast("encoder", new StringEncoder());
ch.pipeline().addLast("decoder", new RequestPayloadDecoder())); //This extends ByteToMessageDecoder
ch.pipeline().addLast("idlehandler", new IdleStateHandler(..));
ch.pipeline().addLast(new DefaultEventExecutorGroup(100),"handler", new
org.apache.camel.component.netty4.handlers.ServerChannelHandler());
将自动读取设置为false
public void channelActive(ChannelHandlerContext ctx) throws Exception {
InetSocketAddress clientAddress = ((InetSocketAddress) ctx.channel().remoteAddress());
ctx.channel().config().setAutoRead(false);
ctx.channel().read();
}
channelRead0()上的处理程序逻辑
public void process(ChannelHandlerContext nettyChannelContext) throws Exception {
if(validate() && channel.isWritable()){
outboundMessages = //get from database
for(List<Outbound> outbound : outboundMessages){
if(channel().isWritable()){
ChannelFuture writeFuture = nettyChannelContext.channel().writeAndFlush(outbound.getString()); //Send the message
writeFuture.sync(); // wait for the IO to complete
logger.info("Updating message status for event row id {}",outbound.getRowId);
updateMessageStatusInDb(outbound);
} } }
ctx.channel.read();//Call read() after writes are done to process next inbound message
}
问题
一切似乎都正常,但间歇性地将其卡在writeFuture.sync()上超过10分钟。我试过使用监听器而不是sync()。但是,需要花费相似的时间来接收operationComplete()回调。
示例日志:
2019-06-18 14:16:39.263 [Camel Thread#20-NettyEventExecutorGroup]信息正在更新事件行ID的消息状态::: 40257
2019-06-18 14:16:39.265 [Camel Thread#20-NettyEventExecutorGroup]信息正在更新事件行ID的消息状态::: 40263
2019-06-18 14:16:39.266 [Camel Thread#20-NettyEventExecutorGroup]信息正在更新事件行ID的消息状态::: 40269
2019-06-18 14:30:09.982 [Camel Thread#20-NettyEventExecutorGroup]信息正在更新事件行ID的消息状态::: 40275
2019-06-18 14:30:09.987 [Camel Thread#20-NettyEventExecutorGroup]信息正在更新事件行ID的消息状态::: 42691
为什么IO需要这么长时间才能完成,对于发送的每30k消息,恰好有40-50条消息。您能提供一些提示吗?