关于client连接调用sync后,写个while死循环发消息,就不走handler里的active了
来源:1-1 Netty深入剖析

慕容5266112
2019-12-16
client连接server,
ChannelFuture channelFuture = bootstrap.connect().sync().addListener(futureListener);
然后在nettyClient.connect("127.0.0.1", 11111, new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { System.out.println(channelFuture.isSuccess()); while (true) { // for (int j = 0; j < 499; j++) { Thread.sleep(2); HotKeyModel hotKeyModel = new HotKeyModel(); hotKeyModel.setAppName("a"); hotKeyModel.setKeyType(KeyType.REDIS_KEY); hotKeyModel.setKey("" + i); channelFuture.channel().writeAndFlush(FastJsonUtils.convertObjectToJSON(hotKeyModel)); } } });
在comleate回调里,写个while死循环给server发消息。消息能发送成功,server也收到了。
但是server给client的消息就收不到了。
而且handler里的channelActive都没走。
如果把while(true)变成for循环,就active也能走,client也能收到server发回来的消息了。
这是什么原因?
1回答
-
闪电侠
2019-12-17
这里的 for 循环,会执行结束的吧,while true 就一直等在这个地方,没有机会去执行 selector 底层的 select 操作来拿到 server 端发来的消息
012019-12-17
相似问题