1 Reply Latest reply on Jul 5, 2006 7:51 PM by saish

    EJB 3.0 Interceptors not Firing

    saish

      I'm sure this is the worst of all possible post types (e.g., no behavior rather than an error that one can diagnose). I am trying to write a very simple EJB 3.0 interceptor that will perform error logging and auditing. The interceptor itself is in a separate class and is referenced via the @Interceptors annotation in a SLSB. When viewing my logs, I never see any of the error messages output.

      Or there might be a simple explanation, "We have not yet implemented interceptors in JBoss-4.0.04-GA". :^)

      My thanks in advance for any assistance anyone can render.

      - Saish



      --- ERRORINTERCEPTOR.JAVA ---

      public final class ErrorInterceptor extends Object {

      @AroundInvoke
      public final Object isDatabaseAlive(final InvocationContext ctx)
      throws Exception {

      // NEITHER OF THESE EVER SHOW UP IN LOGS
      System.err.println("DEBUG >>> GOTCHA!!!");
      LOG.error("ErrorInterceptor executing method " +
      ctx.getMethod().toGenericString());
      try {
      return ctx.proceed();
      }
      catch (Throwable cause) {
      ErrorHandler.getInstance().process(cause);
      if (cause instanceof RuntimeException) {
      throw (RuntimeException) cause;
      }
      if (cause instanceof Error) {
      throw (Error) cause;
      }
      if (cause instanceof Exception) {
      throw (Exception) cause;
      }
      throw new UnexpectedError("Unknown exception received", cause);
      }
      }
      }

      --- HEARTBEATIMPL.JAVA ----

      @Stateless
      @Interceptors ({xxx.xxx.xxx.core.aspect.aop.ErrorInterceptor.class})
      @Local ({Heartbeat.class})
      @Remote ({Heartbeat.class})

      @LocalBinding (jndiBinding="local:xxx/xxx/core/aspect/remote/Heartbeat")
      @RemoteBinding (jndiBinding="remote:xxx/xxx/core/aspect/remote/Heartbeat")

      public final class HeartbeatImpl extends Object
      implements Heartbeat {

      public final boolean isDatabaseAlive() throws Exception {

      if (true) {
      throw new Exception("Foo bar!!!"); // TO TEST ERROR HANDLER
      }
      try {
      if (dataSource == null) {
      return false;
      }
      Connection conn = dataSource.getConnection();
      if (conn == null) {
      return false;
      }
      conn.close();
      return true;
      }
      catch (SQLException e) {
      e.printStackTrace();
      return false;
      }
      catch (RuntimeException e) {
      e.printStackTrace();
      return false;
      }
      }
      }

      -- STACK TRACE (Note: error is forced by code above) ---

      2006-07-05 15:12:32,392 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/mortrac].[jsp]] Servlet.service() for servlet jsp threw exception
      java.lang.Exception: Foo bar!!!
      at xxx.xxx.xxx.core.aspect.remote.HeartbeatImpl.isDatabaseAlive(HeartbeatImpl.java:52)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
      at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
      at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)

        • 1. Re: EJB 3.0 Interceptors not Firing
          saish

          Okay, I finally figured out a solution. Apparently, I had slightly older jboss-ejb3x.jar and jboss-ejb3.jar file versions. They referenced javax/ejb/InvocationContext. Thus, the annotations that I referenced in my compiled source were never located when the container was instead (apparently) trying to find javax/interceptors/InvocationContext. So, my interceptors never fired.

          The solution is easy. Make sure you have the 4.0.4-GA app server jar's listed above in your CLASSPATH for your IDE.

          - Saish