java - Netty TCP Client async messages -
i building tcp client receive , sending messages. followed steps on netty user guide , wrote simple tcp client custom handler extending channelinboundhandleradapter
.
in hander store channelhandlercontext
:
@override public void channelactive (channelhandlercontext ctx) throws exception { super.channelactive (ctx); this.ctx = ctx; }
then have send method uses channelhandlercontext
send messages:
public void sendmessage (string msg) { if (ctx == null) { return; } channelfuture cf = ctx.write (unpooled.copiedbuffer (msg, charsetutil.utf_8)); ctx.flush (); }
the other option have found use channel
object in client class
channel.writeandflush (msg);
i need call send method different thread. best way ?
thanks in advance.
both channelhandlercontext
, channel
thread safe, can write thread without worrying.
if use channel.write()
, message have travel through complete pipeline. if use channelhandlercontext.write()
have travel through upstream handlers in pipeline. therefore writing channelhandlercontext
more efficient.
and note of time better use writeandflush()
instead of write()
.
have @ this presentation more details.
Comments
Post a Comment