2 Replies Latest reply on May 24, 2011 3:20 PM by vaidya1973

    JBOSS and ThreadLocal

    vaidya1973

      Hi,

       

      I have a scenario wherein I have a jersey servlet talking to ejbs within the same ear . I needed to pass in some session info. from the REST servlets to the ejbs and one way in which I did this was to use ThreadLocal variables, populated from within a ContainerFilter (servlet filter). The jersey resources then does a lookup of the ejbs and makes the method calls , within the method call at one pt I need the recorded date but when i look for it , it goes to the initialValue , even though the thread id is the same and it should get the value from the ContainerFilter that i used on its way in.

       

      This set up used to work but i had to introduce some things in the jboss.xml for some other issue and am wondering whether that is causing the ThreadLocal values to get messed up. I introduced a section on enterprise-beans as follows

       

      <enterprise-beans>

      <session>

      <ejb-name>EJBService</ejb-name>

      <ignore-dependency>

      <injection-target>

      <injection-target-class>Target class  </injection-target-class>

      <injection-target-name>Target Service </injection-target-name>

      </injection-target>

      </ignore-dependency>

      </session>

       

      </enterprise-beans>

      Following is my Entity Auditor..

       

      public class EntityAuditor implements Serializable{

       

              private static InheritableThreadLocal<HistoryInfo> auditInfo= new InheritableThreadLocal < HistoryInfo > () {

              @Override protected HistoryInfo initialValue() {

                  System.out.println("Initialized");

                  return new HistoryInfo();

          }

       

      };

       

            public static void setRecordedDate(Date theRecordedDate) {

              System.out.println("Current Thread Id"+Thread.currentThread().getId());

              HistoryInfo audInfo=auditInfo.get();

              audInfo.setRecordedDate(theRecordedDate); //Setting this to the recorded date on the way in..

              auditInfo.set(audInfo);

          }

       

          public static Date getRecordedDate() {

              System.out.println("Current Thread Id"+Thread.currentThread().getId());

       

              HistoryInfo audInfo=auditInfo.get();  // this one should not go to initialValue method but it goes..

              if(audInfo!=null)

              {

                  return audInfo.getRecordedDate();

              }

              return null;

          }

      }

       

       

      Has anyone seen this issue?

        • 1. JBOSS and ThreadLocal
          wdfink

          I use also ThreadLocal and for me it works with SLSB's.

          Do you route the instance of entity or how do you access the ThreadLocal?

          • 2. Re: JBOSS and ThreadLocal
            vaidya1973

            Thank you very much Wolf for the prompt reply. However I found the problem. The problem was that there was a common library that had the  above code and there were two jars of it - one in the lib folder of the war and another in the ear (at the ear level). I was putting the  info in the war and when i was getting it in the other side in the jar(the ejb jar) it wasnt the same map. I took the jars from the lib folder and added it only in the ear level and changed the manifest files to refer to them everywhere and that fixed the problem.

             

            The TheadLocal variable works but I did realize that the ThreadLocal is nothing but a map of Thread id and the object that it uses and so when i changed my filter to put in the request in a static map and delete it from the map on the container response i realized that it wasnt the same map either which helped me to evantually trouble shoot the problem.