3 Replies Latest reply on Dec 14, 2006 12:24 PM by J-C jc

    Permanent interceptor

    Aleksander Bai Newbie

      Scenario: A JBoss server with several beans deployed. Several client access these beans.

      Goal: Intercept EVERY call from a client to a bean, and process some global information, before letting the call proceed or terminate. The objects/class intercepting the call to the various bean must be able to communicate with each other, OR be a common object.

      Problem: What is the best and possible approaches to this?

      I've thought about adding a new AOP layer, and let each bean ie. add a notation. However, each bean would then have it's own instance fo the aop class, right? I want the aop interceptor to make decisions based on all incoming calls.

        • 1. Re: Permanent interceptor
          J-C jc Apprentice

          I have done something like this to catch unexpected exception and perform some action before sending the exception to the client.
          I use interceptor taht I have added in ejb3-interceptors-aop.xml.

          I'm quite happy with that.

          • 2. Re: Permanent interceptor
            Aleksander Bai Newbie

            Sounds like the way to go.

            Do you have any tutorial or examples on how to do this? Have search google and the jboss sites, without finding anything really usefull.

            • 3. Re: Permanent interceptor
              J-C jc Apprentice

              Hi,

              I'm not sure you can simply add your own domain in the ejb3 interceptors. I just connect on existing one.
              If your need is to add your own domain check AOP documentation. From what I remenber it is explain but not so simple to implement.

              It looks like that :

              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE aop PUBLIC
              "-//JBoss//DTD JBOSS AOP 1.0//EN"
              "http://www.jboss.org/aop/dtd/jboss-aop_1_0.dtd">


              ...


              <!--################################################### -->
              <!--My Interceptor-->


              <!--typedef name="bmx" expr="class(@javax.ejb.Remote) OR class(@javax.ejb.Local)"/-->

              <!-- FIN MyInterceptor-->
              <!-- ################################################### -->




              <!-- ################################################### -->
              <!-- My Interceptor-->

              <!--interceptor-ref name="fr.biomerieux.interceptor.ExecutionTimeInterceptor"/-->
              <interceptor-ref name="fr.biomerieux.interceptor.ExceptionHanlderInterceptor"/>

              <!-- FIN My Interceptor-->
              <!-- ################################################### -->


              <interceptor-ref name="org.jboss.ejb3.asynchronous.AsynchronousInterceptor"/>
              ...



              <!-- ################################################### -->
              <!-- My Interceptor-->

              <!--interceptor-ref name="fr.biomerieux.interceptor.ExecutionTimeInterceptor"/-->
              <interceptor-ref name="fr.biomerieux.interceptor.ExceptionHanlderInterceptor"/>

              <!-- FIN My Interceptor-->
              <!-- ################################################### -->

              <interceptor-ref name="org.jboss.ejb3.asynchronous.AsynchronousInterceptor"/>
              ...

              And a intercptor:

              public class ExecutionTimeInterceptor implements Interceptor {
              /**
              * Version number
              */
              private final Logger LOGGER = Logger.getLogger(ExecutionTimeInterceptor.class
              .getName());

              /**
              * Log the elapsed time of the execution of a method
              */
              public Object invoke(Invocation invocation) throws Throwable {
              long start = System.currentTimeMillis();
              try {
              return invocation.invokeNext();
              } finally {
              long end = System.currentTimeMillis();
              MethodInvocation methodInvocation = (MethodInvocation) invocation;
              Method m = methodInvocation.getMethod();
              LOGGER.fine(MessageFormat.format(MESSAGE.METHOD_EXECUTION,
              new Object[] { m.getDeclaringClass().getName(), m.getName(),
              ((end - start) / 1000.) }));
              }
              }

              /**
              * Return the name of the interceptor
              */
              public String getName() {
              return ExecutionTimeInterceptor.class.getName();
              }

              }