2 Replies Latest reply on Jul 29, 2009 7:14 PM by Arbi Sookazian

    Intercepting method calls on non EJB Seam component

    D. B. Newbie

      Hello,


      I have my seam components (View Backed Beans) and i would like to intercept each method call. How can i do that ?


      Am aware of section 6 in the documentation but it looks like it works only when my seam components are EJBs and i don't want to have them so.


      Thanks for any help.

        • 1. Re: Intercepting method calls on non EJB Seam component
          Arbi Sookazian Master

          You can even use Seam interceptors with JavaBean components, not just EJB3 beans!

          No example cited.


          So I created one.  No EJBs.


          ProfilingInterceptor.java:


          public class ProfilingInterceptor {
               
               Log log = Logging.getLog(ProfilingInterceptor.class); 
          
              @AroundInvoke
              public Object profile(InvocationContext ic) throws Exception {
                   
                   log.info("*** Entering method: " + ic.getMethod().getName());
                  
                  long startTime = 0;
                  long endTime = 0;
                  try {
                      startTime = System.currentTimeMillis();            
                      return ic.proceed();
                  } finally {
                      endTime = System.currentTimeMillis();
                      log.info("*** Method " + ic.getClass() + "." + ic.getMethod() + " executed in " + (endTime - startTime) + "ms ***");
                  }
              }
          }



          ProfileMe.java:


          @Target(ElementType.TYPE)
          @Retention(RetentionPolicy.RUNTIME)
          @Interceptors(ProfilingInterceptor.class)
          public @interface ProfileMe {}



          TestStartupInterceptor.java:


          @Name("startupInterceptor")
          @ProfileMe
          @Scope(ScopeType.APPLICATION)
          @Startup
          public class TestStartupInterceptor {
          
               @Create
               public void init(){
                    try{
                         Thread.sleep(5000);
                    }
                    catch(Exception e){
                         //no op
                    }
               }
          }




          09:58:45,552 INFO  [ProfilingInterceptor] *** Entering method: init
          09:58:50,552 INFO  [ProfilingInterceptor] *** Method class org.jboss.seam.intercept.EE5SeamInvocationContext.public void org.jboss.seam.example.booking.TestStartupInterceptor.init() executed in 5000ms ***


          • 2. Re: Intercepting method calls on non EJB Seam component
            Arbi Sookazian Master

            https://jira.jboss.org/jira/browse/JBSEAM-4327


            Add example code of how to use Seam interceptors with JavaBean components in ref doc