import javax.ejb.Stateful;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

@Stateful
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {

  public EmployeeBean() {
  }

  public void doAction() {
    System.out.println("Processing...");
  }

  @AroundInvoke
  public Object TimerLog(InvocationContext ctx) throws Exception {
    String beanClassName = ctx.getClass().getName();
    String businessMethodName = ctx.getMethod().getName();
    String target = beanClassName + "." + businessMethodName;
    long startTime = System.currentTimeMillis();
    System.out.println("Invoking " + target);
    try {
      return ctx.proceed();
    } finally {
      System.out.println("Exiting " + target);
      long totalTime = System.currentTimeMillis() - startTime;
      System.out.println("Business method " + businessMethodName + "in " + beanClassName + "takes "
          + totalTime + "ms to execute");
    }
  }
}

Share and Enjoy:
  • Facebook
  • PDF
  • RSS
  • Print
  • Twitter
  • Digg
  • LinkedIn
  • del.icio.us
  • StumbleUpon
  • Google Bookmarks
  • Technorati