开发者

how to close the Channel for Writing

We are using Netty 3.2.3 version and want to close the channel from writing according to a certain calculation.

We are closing the channel by:

channel.setInterestOps(channel.getInterestOps() | Channel.OP_WRITE);

and re-opening it by:

channel.setInterestOps(channel.getInterestOps() & Channel.OP_WRITE);

But according to the performance something is wrong. the calculation is the same as开发者_开发技巧 we are doing serReadable true/false, that is working fine.


Channel.OP_WRITE is for information only. Netty uses it to signal whether the I/O thread can write data to the channel immediately. If channel.isWritable() is false then Netty will queue any further data until it can be accepted by the channel. If you continue to write data when isWritable() is false then you may eventually encounter an OutOfMemoryError.

I assume you don't want to actually close the channel, just stop writing to it. The correct approach is to either check channel.isWritable() after each send, or receive ChannelStateEvent (possibly by overriding SimpleChannelUpstreamHandler.channelInterestChanged()) and pause whatever application process you have that is writing data to the channel.

In either case you'll want to receive ChannelStateEvent to know when you can start writing to the channel again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜