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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:06:47+00:00 2026-06-14T21:06:47+00:00

I have created an Interceptor with a method annotated with @AroundInvoke to log method

  • 0

I have created an Interceptor with a method annotated with @AroundInvoke to log method calls and associated timings. This is based on Richard Hightowers excellent blog posting on CDI AOP http://java.dzone.com/articles/cdi-aop):

@AroundInvoke
public Object log(InvocationContext ctx) throws Exception {
    Logger logger = Logger.getLogger(ctx.getTarget().getClass().getName());

    logger.trace("ENTERING : "+ctx.getMethod());

    long start = System.currentTimeMillis();

    Object returnMe = ctx.proceed();

    long executionTime = System.currentTimeMillis() - start;

    logger.trace("EXITING : "+ctx.getMethod()+":"+executionTime+"ms");

    return returnMe;
}

I would like to log the user’s session id so I can readily analyse a users path through the application. How do I get hold of the user’s session?

I looked at injecting the SessionContext but could not see how to use the API to extract this value.

I have looked at log4j MDC but I was hoping to avoid the need to add a servlet filter.

  • 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-14T21:06:48+00:00Added an answer on June 14, 2026 at 9:06 pm

    Really my question should have been “How do I log method invocations such that a given users path through the application can easily be parsed out of the log file?”. The question as it stands assumes getting the session id is the answer which may well not be the case. With this better phrased question in mind I have now discovered that as long as all business logic of concern is accessed via EJBs you can indeed log a given users path as follows:

    Create an interceptor class that has an EJB SessionContext injected in to it:

    /**
    * SessionContext of this EJB; this will be injected by the EJB
    * Container because it's marked w/ @Resource
    */
    @Resource
    private SessionContext context;
    

    Add a method annotated with AroundInvoke that will intercept calls:

    @AroundInvoke
    public Object log(InvocationContext ctx) throws Exception {
        String originName = Thread.currentThread().getName();
        String currentUser = context.getCallerPrincipal().getName();
            try{
               String tracingName = currentUser + " " + originName;
               Thread.currentThread().setName(tracingName);
               return ctx.proceed();
            }finally{
                Thread.currentThread().setName(originName);
            }
        }
    

    Note we use the session context to get hold of the user who has caused this action. We then change the name of the thread to hold that users name. All subsequent logging in this call stack will output the users name assuming loggin is configured to also output the thread name regardless of if they are in an EJB or just some POJO class.

    This thread renaming was inspired by Adam Bien’s server independent thread tracking utility:

    http://www.adam-bien.com/roller/abien/entry/server_independent_thread_tracking_utility

    Altering it to rename the thread to be the users name was a modification I made.

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

Sidebar

Related Questions

I have created an android application that calls (using kSOAP library) a SOAP based
I have a method that created a NSURLRequest. If I load this NSURLRequest in
I have created a Authentication Interceptor in struts2. I have to check that when
I have created a QWidget with a bunch of QToolButtons in it and I
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created some JQuery that will expand a div 'popup' on hover and
I have created a JSP / servlets application running in Tomcat 7. It runs
I have created an EDMX in visual studio 2010 SP1. It has been built
I have created a custom post type named People. I have created a page
I have created a function that shows/hides different messages according to a combination 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.