I'm looking for a way to add an attribute to a Channel
before creating it.
The reason is that I need to associate the Channel
to some arbitrary object so that I have the ability to get the object from the channel (e.g. in the channelActive()
method where I have the Channel
and I need the associated object).
The Bootstrap
exposes an attr() method but an attribute that would be added this way is available to all channels created from this Bootstrap
instance.
Also, adding the attribute after Channel creation is problematic since the channelActive()
method may be called before the attribute is added.
Adding via
Bootstrap.attr(...)
will make the attribute available inchannelActive(...)
as the attribute is set beforefireChannelActive()
is called.If you do not want to use this method you can also just add a
ChannelInboundHandler
that overrideschannelRegistered(...)
and add the attribute there by yourself.For who needs it, here is a solution:
Instead of relying on the
Bootstrap
to create theChannel
, the code above creates and registers theChannel
, and connects using theChannel
interface.