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 8201413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:48:20+00:00 2026-06-07T06:48:20+00:00

I have a synchronization issue regarding a bind request and a upstream handler that

  • 0

I have a synchronization issue regarding a bind request and a upstream handler that receives a channelBound event. I need to attach an object to the channel before the handler can ever receive the channelBound event due to the fact that the handler needs to use the object to handle the callback. Example below.

Handler example:

public class MyClientHandler extends SimpleChannelUpstreamHandler {

    @Override
    public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e) {

        /* Problem: This can occur while the channel attachment is still null. */
        MyStatefulObject obj = e.getChannel().getAttachment();

        /* Do important things with attachment. */ 
    }

}

Main example:

ClientBootstrap bootstrap = ... //Assume this has been configured correctly.

ChannelFuture f = bootstrap.bind(new InetSocketAddress("192.168.0.15", 0));

/* It is possible the boundEvent has already been fired upstream
 *  by the IO thread when I get here. 
 */
f.getChannel().setAttachment(new MyStatefulObject());

Possible Soultions

I’ve come up with a couple of solutions to get around this but they both kind of “smell” which is why I’m here asking if anyone has a clean way of doing this.

Solution 1: Spin or block in the channelBound callback until the attachment is not null. I don’t like this solution because it ties up an I/O worker.

Solution 2: Make MyClientHandler in to a bi-directional handler and get the attachment using a ThreadLocal in a bindRequested downstream callback. I don’t like this because it relies on a Netty implementation detail that the requesting thread is used to fire the bindRequested event.

I find solution 1 to be more tolerable than solution 2. So if that is what I need to do I will.

Is there an easy way to get a channel reference without requesting a bind or connect first?

  • 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-07T06:48:22+00:00Added an answer on June 7, 2026 at 6:48 am

    Yes it is possible that, boundEvent can get the handler before you set the attachment to the channel.

    If the attachment is very specific to every channel your opening, then you can register a channel future listener on bind future and set the attachment on operationComplete()
    by setting up everything without using BootStraps. Following is a modified version of EchoClient Example, It works fine.

           // Configure the client.
        final NioClientSocketChannelFactory clientSocketChannelFactory = new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool());
    
    
        // Set up the pipeline factory.
        final ChannelPipelineFactory channelPipelineFactory = new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(
                        new MyClientHandler());
            }
        };
    
        ChannelPipeline pipeline = channelPipelineFactory.getPipeline();
        final Channel channel = clientSocketChannelFactory.newChannel(pipeline);
    
        channel.getConfig().setPipelineFactory(channelPipelineFactory);
        channel.getConfig().setOption("tcpNoDelay", true);
        channel.getConfig().setOption("receiveBufferSize", 1048576);
        channel.getConfig().setOption("sendBufferSize", 1048576);
    
        ChannelFuture boundFuture = Channels.future(channel);
    
        boundFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    future.getChannel().setAttachment(new Object());// set the channel attachment
                }
            }
        });
    
    
        channel.getPipeline().sendDownstream(new DownstreamChannelStateEvent(channel, boundFuture, ChannelState.BOUND, new InetSocketAddress(host, 0)));
    
        ChannelFuture connectFuture = Channels.future(channel); 
        channel.getPipeline().sendDownstream(new DownstreamChannelStateEvent(channel, connectFuture, ChannelState.CONNECTED, new InetSocketAddress(host, port)));
    
        channel.getCloseFuture().awaitUninterruptibly();
    
        clientSocketChannelFactory.releaseExternalResources();// do not forget to do this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a doubt regarding Java Synchronization . I want to know if I
I have a fairly straightforward WCF service that performs one-way file synchronization for a
I have a piece of code that handles the multi-threading (with shared resources) issue,
Suppose I have a custom collection class that provides some internal thread synchronization. For
I have a legacy (solve this problem for us issue) app that communicates with
Since there is the Double-checked locking issue so we have to use synchronization to
I think I have a synchronization problem...It may be too basic..Please help.. I have
I have a multithreaded program which uses pthread mutexes for synchronization on few integers.
What do you use for data synchronization? I have been working with Microsoft Sync
have written this little class, which generates a UUID every time an object of

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.