关于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 端发来的消息

0
1
慕容5266112
经过多次尝试,发现while true时,有时会active,有时不会active。只要走了active就能收到服务端消息,不active了就收不到。
2019-12-17
共1条回复

Java读源码之Netty深入剖析

解析netty各大组件细节,百万级性能调优,设计模式实际运用

2334 学习 · 283 问题

查看课程