Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to Netty channel without ChannelFuture

Tags:

java

netty

Is it possible to write to Netty channel without creating unnecessary ChannelFuture? (Without generating unnecessary object for GC...)

like image 528
Eirenliel Avatar asked Feb 22 '23 18:02

Eirenliel


2 Answers

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.

like image 54
trustin Avatar answered Mar 06 '23 04:03

trustin


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.

like image 32
Norman Maurer Avatar answered Mar 06 '23 04:03

Norman Maurer