Is it possible to write to Netty channel without creating unnecessary ChannelFuture? (Without generating unnecessary object for GC...)
If you really wish you don't want to create a ChannelFuture
, you can do this for Netty 3:
Channels.write(ctx, Channels.succeededFuture(channel), message);
Channels.succeededFuture(..)
returns a channel-local singleton object. However, you should never add a listener to the returned future because it's already complete.
In Netty 4, you can use void promise:
ctx.write(msg, ctx.voidPromise());
ChannelHandlerContext.voidPromise()
returns a dummy singleton promise which is never fulfilled. The operations like adding a listener or waiting until it is fulfilled will fail immediately.
No... a ChannelFuture will be created all the time. But a ChannelFuture is cheap and small, so I think there are better places to look for optimizations.
I think more important is to call Channel.write(..) as less as possible as writing can be expensive in terme of system calls. So if you need to send multiple buffers you may put them all in one and just call Channel.write(...) one time and not for example 5 times.
In SMTP this can be done for example if the SMTP server supports the PIPELINING extension and so make optimal use of the resources.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With