Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto get some ID for a Netty Channel?

Tags:

netty

Channel.id() has been removed in Netty 4.0.0.CR9.

How do I get an ID of a Channel these days?

I used to use the id() for logging purposes in all my Handlers (E.g. System.out.println(ctx.channel().id() + " - readableBytes(): " + in.readableBytes())).

I cannot rely on the toString method of a Channel because that might be overridden.

like image 958
AndrewBourgeois Avatar asked Aug 15 '13 22:08

AndrewBourgeois


2 Answers

If you are sure that the channel is active, you can generate the unique ID of the channel by combining the hashCode(), remoteAddress(), and localAddress(). Alternatively, you can simply use Channel.toString() which generates a string from the three properties. If the channel is not active yet, remoteAddress() and localAddress() will not give meaningful information, so you are still at the risk of collision.

Netty 4.1 and 5.0 re-introduced Channel.id(), which returns a new type called ChannelId. It uses various information such as MAC address, current PID, timestamp, and hashcode, so that it can even be used as a globally unique ID. Check the Javadoc.

like image 146
trustin Avatar answered Oct 17 '22 16:10

trustin


You can use Channel.hashCode() . We removed id() as it was not guaranteered to be 100 % unique.

like image 20
Norman Maurer Avatar answered Oct 17 '22 16:10

Norman Maurer