0 Replies Latest reply on Jul 24, 2014 1:42 PM by rsisto

    Pass data between Interceptors and Asynchronous methods

    rsisto

      Hi, I recently asked this question in stackoverflow with not so great responses, but finally arrived to a solution.

      http://stackoverflow.com/questions/24809196/how-to-pass-data-from-ejb-to-async-ejb/24940141#24940141

      I wanted to ask the experts in JBoss development if the final solution would have any imlpications or problems.

      I am using JBoss EAP 6.1

       

      Let's get to the point. I need to develop 2 interceptors, which will intercept 2 EJB 3.1 SLSB, something like this:

       

      @Stateless
      @Interceptors(InterceptorA.class)
      public class StatelessA{
        ...
        public void methodA(){
          statelessB.methodB();
        }     
      
      @Stateless
      @Asynchronous
      @Interceptors(InterceptorB.class)
      public class StatelessB{
        public void methodB(){...
      

       

      I will create some data in InterceptorA, which needs to be available in InterceptorB. The problem is that since InterceptorB and methodB run in a different thread as methodA, since this is an asynchronous invocation.

      Of course, my first thought was that I couldn't use a ThreadLocal variable, since these are not propagated to the asynchronous thread.

       

      I read the EJB 3.1 specification, which states that the security context is propagated, so I thought I could use that for sending my information.

      I solved it as follows:

       

      In InterceptorA:

       

      SecurityContext securityContext = SecurityContextAssociation.getSecurityContext();
      securityContext.getData().put("interceptorAData",data);
      

      In InterceptorB:

       

      SecurityContext securityContext = SecurityContextAssociation.getSecurityContext();
      securityContext.getData().get("interceptorAData");
      

       

       

      That way I can pass the info correctly from interceptorA to B. Can somebody tell me if there's a better solution?

      I know this implementation is coupled with JBoss server, but it decouples my application from the interceptors, which is what I want.