13 Replies Latest reply on Apr 30, 2008 8:49 AM by stalep

    Can i have interceptor on throw exception?

      Hey,

      In some point the transaction is rollback, but i dont see any exception in the log.

      so i think to add (if it possible) an intercetor that will be called when an exception is thrown , is it possible?

      Thank you

        • 1. Re: Can i have interceptor on throw exception?
          flavia.rainone

          Hi,

          You can do this with the new after-throwing typed advice. Examples can be found in our tutorial, and a more detailed documentation is available in our User Guide.

          If you are using 1.5GA, you will have to wait till our next 2.0.0GA release, which fortunately is scheduled for the end of this week. If not, the feature is available in our CR releases.

          • 2. Re: Can i have interceptor on throw exception?

            I am working with Jboss4.2 , so i guess it dosnt support.

            do you have an idea how can i intercept the transaction?

            Thank you

            • 3. Re: Can i have interceptor on throw exception?
              stalep

              yes, you can just add an interceptor that catches exceptions. i made an ejb3 interceptor for a client some time ago that was just a "wrapper" for exceptions. eg, if an exception was thrown the interceptor would catch it, parse it, and then throw a checked in-house exception ( we didnt want the client to be given ejb3 exceptions).
              here is a sample of it:

              import javax.interceptor.AroundInvoke;
              import javax.interceptor.InvocationContext;
              
              public class FooSystemExceptionInterceptor
              {
               @AroundInvoke
               public Object intercept(InvocationContext ctx) throws Exception
               {
               long start = System.currentTimeMillis();
               try
               {
               return ctx.proceed();
               }
               catch(RuntimeException re)
               {
               System.out.println("Original Exception:");
               re.printStackTrace();
               throw handleException(re);
               }
               finally
               {
               System.out.println("Facade call "+ctx.getMethod().getName()+" took: "+(System.currentTimeMillis()-start));
               }
               }
              
               private FooSystemException handleException(RuntimeException e)
               {
               ....
               return mySystemException;
               }
              
              }

              then you just add this interceptor to the beans you want to intercept. let me know if this solves (or not) your problem :)

              • 4. Re: Can i have interceptor on throw exception?

                I think i didnt was clear, so please let me explain again the problem.

                I have a Statless bean with method - doProcess(){
                processA();
                entitymanager.find(XXXX);
                }

                the call to entitymanager.find failed, because it complains that the transaction was marked for rollback.

                My problem is to find the poinf where the transaction marked for rollback.
                In my log i dont found any exception, so maybee some where in processA() an exception is catched without logging it or throw it again.

                I think on using AOP in order to find this point- or by intercept the transaction (but this is jboss object , so i might have problem) or by add (if it possible) pointcut on any throw exception in the system.

                Any suggestions?

                10x

                • 5. Re: Can i have interceptor on throw exception?
                  stalep

                  you cant use "normal" aop on ejb beans since they are changed when deployed on jboss. but if you use ejb3 you can use the interceptor util thats included in the spec to do what you ask for.
                  so since you call entitymanager.find(..) im guessing you are using ejb3. if you are take a look at http://docs.jboss.org/ejb3/app-server/tutorial/interceptor/interceptor.html.

                  • 6. Re: Can i have interceptor on throw exception?

                    I know how to add interceptor to ejb. this is not the problem.
                    the problem is how to catch any throw exception in the system or intercept tranaction.rollback

                    • 7. Re: Can i have interceptor on throw exception?
                      stalep

                      the code i provided above will catch any exception thrown in a method that is intercepted. just add the above interceptor to the methods that can/will throw an exception and it will catch it.

                      • 8. Re: Can i have interceptor on throw exception?

                        Thanks for your answer.

                        The method call to - > processA , which call to xxx() wich call to yyy()...

                        somewhere in the middle for example in yyy() an exception (rollbak exception) is thronw and catched , so the code is not usefull.

                        i need to know where in my code (if there is) exception is thrown and catched without throw it to the caller level.

                        Thank you

                        • 9. Re: Can i have interceptor on throw exception?
                          stalep

                          im not sure that i completely understand what you mean, but here we go :)
                          you have a method yyy() that throws an exception which again triggers a rollback and an exception is then again thrown on the method thats "outside" of the transaction (correct?).
                          which exception do you want to catch? the rollback or the exception that triggers the rollback. both are possible to catch.
                          the code example that i provided is just an example of how to throw another exception than the one that was thrown. you can just alter it to not throw an exception if that is what you want.

                          • 10. Re: Can i have interceptor on throw exception?

                            My puprpose is for debugging my code.

                            some where in my code an exception is caught and not thrown and not write in the log, i need to find this place.

                            if i can add a pointcut on any exception that is thrown in the system it can help me to find this place.

                            • 11. Re: Can i have interceptor on throw exception?
                              stalep

                              then you can just add that interceptor to every ejb3 bean you have and it will catch it :)
                              - if you also have some pojos that might throw an exception you have to add a interceptor on those with "plain old" aop.

                              • 12. Re: Can i have interceptor on throw exception?

                                and this is exctly the question- how i add for pojo pointcut on all throw exception.
                                i want to add it for any pojo in the system.

                                Thank you for your answer.

                                • 13. Re: Can i have interceptor on throw exception?
                                  stalep

                                  with aop 1.5 you have to add a around pointcut to every method in your code that can throw an exception. the interceptor will be something like:

                                  public class CatchExceptionInterceptor implements Interceptor
                                  {
                                   public String getName() { return "CatchExceptionInterceptor"; }
                                  
                                   public Object invoke(Invocation inv) throws Throwable
                                   {
                                   try
                                   {
                                   return inv.invokeNext();
                                   }
                                   catch(Exception e)
                                   {
                                   logExceptionHere();
                                   return e;
                                   }
                                   finally
                                   {
                                   blabla
                                   }
                                   }
                                  }

                                  and then you need to add pointcuts that binds this interceptor to all you methods/construcors (i guess thats all that can throw an exception) eg:
                                  <bind pointcut="execution(* * org.foo.*->*(..))">
                                   <interceptor class="CatchExceptionInterceptor"/>
                                  </bind>
                                  <bind pointcut="whatever_that_fits...">
                                   <interceptor class="CatchExceptionInterceptor"/>
                                  </bind>

                                  - i wouldnt recommend this for other than testing though :)