Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:03:44+00:00 2026-06-10T20:03:44+00:00

I’m trying to pass an object from one handler to another using the ChannelContext.attr()

  • 0

I’m trying to pass an object from one handler to another using the ChannelContext.attr() method:

private class TCPInitializer extends ChannelInitializer<SocketChannel>
{
    @Override
    public void initChannel(final SocketChannel ch) throws Exception
    {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new ReadTimeoutHandler(mIdleTimeout));
        pipeline.addLast(new HostMappingHandler<SyslogHandler>(mRegisteredClients,
                                                               mChannels));
        pipeline.addLast(new DelimiterBasedFrameDecoder(MAX_FRAME_SIZE,
                         Delimiters.lineDelimiter()));
        pipeline.addLast(new Dispatcher());
    }
}

The HostMappingHandler is a template class that maps hosts to data:

public class HostMappingHandler<T> extends ChannelStateHandlerAdapter
{

    public static final AttributeKey<HostMappedObject> HOST_MAPPING =
        new AttributeKey<HostMappedObject>("HostMappingHandler.attr");


    private final ChannelGroup mChannels;
    private final Map<String, T> mMap;


    public HostMappingHandler(Map<String, T> registrations,
                              ChannelGroup channelGroup)
    {
        mChannels = channelGroup;
        mMap = registrations;
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception
    {
        SocketAddress addr = ctx.channel().remoteAddress();
        T mappedObj = null;

        if (addr instanceof InetSocketAddress)
        {
            String host = ((InetSocketAddress) addr).getHostName().toLowerCase();
            mappedObj = mMap.get(host);
        }

        if (mappedObj != null)
        {
            // Add the channel to the list so it can be easily removed if unregistered
            mChannels.add(ctx.channel());

            // Attach the host-mapped object
            ctx.attr(HOST_MAPPING).set(new HostMappedObject<T>(mappedObj));
        }
        else
        {
            log.debug("Bad host [" + addr + "]; aborting connection request");
            ctx.channel().close();
        }
        super.channelRegistered(ctx);
    }

    @Override
    public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception
    {
        // Find the parser for this host. If the host is no longer registered,
        // disconnect the client.
        if (ctx.channel().remoteAddress() instanceof InetSocketAddress)
        {
            InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
            T handler = mMap.get(addr.getHostName().toLowerCase());

            if (handler == null)
            {
                log.debug("Host no longer registered");
                ctx.channel().close();
            }
            else
            {
                log.debug("Sanity Check: " + ctx.attr(HostMappingHandler.HOST_MAPPING).get());
            }
        }
        super.inboundBufferUpdated(ctx);
    }

    // ==================================================

    public static class HostMappedObject<C>
    {
        private final C mObj;

        public HostMappedObject(final C in)
        {
            mObj = in;
        }

        public C get()
        {
            return mObj;
        }
    }
}

And the dispatcher is currently very simple:

private static class Dispatcher extends ChannelInboundMessageHandlerAdapter<Object>
{
    @Override
    public void messageReceived(final ChannelHandlerContext ctx,
                                final Object msg)
            throws Exception
    {
        HostMappingHandler.HostMappedObject wrapper =
                ctx.attr(HostMappingHandler.HOST_MAPPING).get();

        log.debug("Received: " + wrapper);
    }
}

Incidentally the initializer and dispatcher are private classes of a “server” object. However when I run a unit test that registers the localhost and attempts to connect to it, it fails and my debug output shows:

HostMappingHandler.inboundBufferUpdated: Sanity Check: test.comms.netty.HostMappingHandler$HostMappedObject@eb017e
Dispatcher.messageReceived: Received: null

So the mapping is definitely preserved between channel registration and receiving an inbound message in the HostMapper class, and investigating a little further in the debugger shows that the attribute map for the context in Dispatcher does have an entry – the problem is that the value is NULL and not the object.

Am I missing something obvious, or just an alpha bug?

Edit:

I’ve worked around the problem for now by attaching the data to the Dispatcher’s context, by having the HostMappingHandler also take a class to attach to. The bug appears to be that setting an attribute in a handler’s ChannelHandlerContext causes the attribute to appear in another handler with the correct key but NULL value. Without knowing the desired workflow, the bug is that either the key shouldn’t appear at all, or the value should not be null.

public HostMappingHandler(final Map<String, T> registrations,
                          final ChannelGroup channelGroup,
                          final Class<? extends ChannelHandler> channelHandler)
{
    mChannels = channelGroup;
    mMap = registrations;
    mHandler = channelHandler;
}

//...

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception
{
    //...
    // Attach the host-mapped object
     ctx.pipeline().context(mHandler).attr(HOST_MAPPING).set(new HostMappedObject<T>(mappedObj));
    //...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:03:46+00:00Added an answer on June 10, 2026 at 8:03 pm

    AttributeMap in ChannelHandlerContext is bound to the context, and that’s why the attribute you set in one context is not visible in other context.

    Please note that Channel also implements AttributeMap. You can just use the attribute map bound to a channel:

    ctx.channel().attr(...) ...;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.