4 Replies Latest reply on Feb 17, 2010 5:07 PM by thomasgo

    Application/EAR level interceptors

    thomasgo

      Hi,

       

      I want to intercept every session bean invocations in my application but I could not yet figure out how to declare EAR level interceptors.

       

      I already tried JAR level interceptors, i.e. putting an ejb-jar.xml in my ejb jars and declaring the interceptor for all session beans in that jar.

      My applications contains multiple ejb jars and some of them are beyond my control, i.e. I can't add or change an ejb-jar.xml there.

      However, I still need to intercept their calls.

       

      Another thing I tried was server level interceptors, i.e. the interceptors were placed inside a jar in the server's lib directory and registered using a xxx-aop.xml file in the server's deploy directory.

      However, this also isn't an option, since there are other applications that will be deployed on the same machine and the interceptor should not be visible to them (and no calls should be intercepted).

       

      A third alternative would be load-time weaving, but that's an option that is not preferred by our server administrators, since it would enable load-time weaving for all deployed applications and would also require to patch our JBoss server (which is version 4.2.3.GA, btw).

       

      So the question remains: Is it possible to declare EAR level interceptors? And if so, how would I do that?

       

      Any help would ver highly appreciated.

       

      Thanks in advance.

       

      Regards,

       

      Thomas

        • 1. Re: Application/EAR level interceptors
          jaikiran

          What kind of interceptors are you planning to write? javax.interceptor.Interceptor or JBoss AOP based interceptors?

           

          ThomasGo wrote:

           

           

          My applications contains multiple ejb jars and some of them are beyond my control, i.e. I can't add or change an ejb-jar.xml there.

          ....

           

          Another thing I tried was server level interceptors, i.e. the interceptors were placed inside a jar in the server's lib directory and registered using a xxx-aop.xml file in the server's deploy directory.

          However, this also isn't an option, since there are other applications that will be deployed on the same machine and the interceptor should not be visible to them (and no calls should be intercepted).

          ...

           

          A third alternative would be load-time weaving, but that's an option that is not preferred

          ...

           


          I don't see an option with all those restrictions.

           

          Can you provide more details on what you are trying? Maybe there's a different way to do it.

          • 2. Re: Application/EAR level interceptors
            thomasgo

            Well,  I've faced

            Ththat problem multiple times so far, but wasn't able to find a way around this yet.

             

            In the current case the primary objective is to provide more logging information:

             

            Since we have multiple applications that might be more or less identical (i.e. different builds for different customers) running on the same server, we want to provide information about the application that generated a log message (e.g. "[SomeClass (App: SomeApp)] message", with SomeApp being an application supplied string ).

             

            Using log4j, I figured out that the best way to do this would be to inject the information into the MDC which can be read by PatternLayout.

            Our application is mostly web-based and thus the first step was to add a servlet filter which sets the information at the beginning of each request and clears it afterwards.

             

            However, we also have processed that aren't initated via http requests, e.g. scheduler jobs or process started via the JMX console.

            We'd also like to add the extra information in those cases, without explicitly putting it into the MDC in each method that can start a process (i.e. MBean methods, job methods etc.). This approach would be tedious and error prone, so we decided an interceptor would be the best way, since each and every call to a service would be intercepted. Thus nobody could bypass/forget that step and we'd have to write our code only once.

             

            There are also a bunch of other applications that might end up running on the same server and those would not be able to provide the information required by a global interceptor.

             

             

            Conceptually, an application-wide interceptor could be used for other tasks that apply to all services of only one application.

            IMHO, the server should not know about any specifics of the application and putting a global interceptor into the server's lib directory for just one application would contradict this.

             

             

            Thanks for your help in that matter.


            • 3. Re: Application/EAR level interceptors
              jaikiran

              ThomasGo wrote:

               


               

               

              Conceptually, an application-wide interceptor could be used for other tasks that apply to all services of only one application.


              You can actually place a jboss-aop.xml file in the .ear/META-INF folder. This jboss-aop.xml will then contain the pointcut expressions to intercept the calls to any bean implementation classes (or any classes for that matter) in that specific application. This is more related to JBoss AOP rather than EJB3. I am assuming you know about JBoss AOP and it's usage. Caveat - i haven't tried this and don't know if you might run into any issues.

              1 of 1 people found this helpful
              • 4. Re: Application/EAR level interceptors
                thomasgo

                Thanks for the hint, I'll try that after my vacation.

                 

                And yes, I knew this topic would also touch JBoss AOP, but since interceptors are part of EJB I thought the topic should go here.