Storing extra properties for each channel

Trustin Lee tlee at redhat.com
Sat Mar 21 10:28:45 EDT 2009


Hi :)

On Sat, Mar 21, 2009 at 6:04 AM, jdv145 <jandevries145 at fastmail.fm> wrote:
>
> I am using the latest beta now and noticed some great additions since last
> stable release :). I feel like a kid with some new cool toys now ^^.
>
> Kudos for that Trustin

You're welcome.  I will be looking forward to your feed back soon. ;)

> Anyways i also got a question about storing some extra info for the
> channels.
>
> I used to store all channels in a concurrent map but since you made a nice
> ChannelGroup now i like to use that. From the FAQ i understand you only need
> to add the channel to a ChannelGroup when a connection is established. After
> the channel is disconnected it is removed automatically from the
> channelgroup (right?).

Right.

> The thing is... i used to had a map with all channels and a seperate map
> with all attribute Objects.
>
>    private ConcurrentHashMap<Integer, Channel> channelsMap; // map
> channel-ID to channel

You don't need this.  Channel's equals() must be implemented like this
by contract: return this == o;

>    private ConcurrentHashMap<Integer, ChannelAttributes>
> channelAttributesMap; //map channel-ID to ChannelAttributes

And therefore, ConcurrentMap<Channel, ChannelAttributes> will just work fine.

> since i now am using the channelsgroup i am wondering what the best way is
> to store an extra attribute-object for each channel. If i use my good old
> channelAttributesMap i still need to handle channeldisconnect and
> channelconnects myself. (i remembered you could store an object in mina for
> each channel but so far i couldn't find something similar for netty yet).
>
> Can you give me some advise on this one?

I'd recommend you to replace ChannelAttributes with ChannelLocal if
you prefer map-lookup approach than other approaches.  It's pretty
similar to ThreadLocal.

The other approaches are:

  * an attachment in ChannelHandlerContext, and
  * a member variable in your ChannelHandler (check the factorial
server example to how to do this.)

HTH,

— Trustin Lee, http://gleamynode.net/




More information about the netty-users mailing list