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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:49:46+00:00 2026-06-15T05:49:46+00:00

How do I implement @SessionScoped in a Netty based TCP server? Creating Custom Scopes

  • 0

How do I implement @SessionScoped in a Netty based TCP server? Creating Custom Scopes is documented in Guice manual, but it seems that the solution only works for thread based and not asynchronous IO servers.

Is it enough to create the Channel Pipeline between scope.enter() and scope.exit()?

  • 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-15T05:49:47+00:00Added an answer on June 15, 2026 at 5:49 am

    Disclaimer : this answer is for Netty 3. I’ve not had the opportunity to try Netty 4 yet, so I don’t know if what follows can be applied to the newer version.

    Netty is asynchronous on the network side, but unless you explicity submit tasks to Executors, or change threads by any other means, the handling of ChannelEvents by the ChannelHandlers on a pipeline is synchronous and sequential. For instance, if you use Netty 3 and have an ExecutionHandler on the pipeline, the scope handler should be upstream of the ExecutionHandler; for Netty 4, see Trustin Lee’s comment.

    Thus, you can put a handler near the beginning of your pipeline that manages the session scope, for example:

    public class ScopeHandler implements ChannelUpstreamHandler {
    
        @Override
        public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) {
    
            if (e instanceof WriteCompletionEvent || e instanceof ExceptionEvent)
                ctx.sendUpstream(e);
    
            Session session = ...; // get session, presumably using e.getChannel()
    
            scope.enter();
            try {
                scope.seed(Key.get(Session.class), session);
                ctx.sendUpstream(e);
            }
            finally {
                scope.exit();
            }
        }
    
        private SessionScope scope;
    
    }
    

    A couple of quick remarks:

    • You will want to filter some event types out, especially WriteCompletionEvent and ExceptionEvent which the framework will put at the downstream end of the pipeline during event processing and wil cause reentrancy issues if not excluded. In our application, we use this kind of handler but actually only consider UpstreamMessageEvents.
    • The try/finally construct is not actually necessary as Netty will catch any Throwables and fire an ExceptionEvent, but it feels more idiomatic this way.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To implement a timer, I'm currently forking a process that uses SIGALRM to periodically
I tried to inject a Managed Bean to another bean, but failed. That is
I often hear people saying that session scoped objects should implement Serializable ? Why
I'm creating HttpSession container this way: @SessionScoped @ManagedBean(name=userManager) public class UserManager extends Tools {
I have this managed bean based on this tutorial . import java.io.Serializable; import javax.enterprise.context.SessionScoped;
I have a UserService that fails with ClassNotFound when using ViewScoped but works with
I am implementing server side pagination using Richfaces, but the implementation using PaginatingDataModel requires
Implement an algorithm to determine if a string has all unique characters. What if
To implement github like fork/pull request function in my project, the auto-merge feature need
I implement notifications in my IOS app, and I use customized sounds. Everything works

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.