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.
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.
You can use Channel.hashCode() . We removed id() as it was not guaranteered to be 100 % unique.
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