Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Interceptor Methods


We will modify the BankServiceBean stateless session bean and add an interceptor which logs the time taken to execute each business method within the bean. We name the interceptor method methodStats(). The code is shown below:

package ejb30.session;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import ejb30.entity.Customer;
import javax.persistence.PersistenceContext;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
@Stateless
public class BankServiceBean implements BankService {
...
@AroundInvoke
public Object methodStats(InvocationContext invctx)
throws Exception {
String name = invctx.getMethod().getName();
long startTime = System.nanoTime();
System.out.println("Starting method: " + name);
try {
return invctx.proceed();
} finally {
long elapsedTime = System.nanoTime() - startTime;
System.out.println("Finished method: " + name +
" Method took " + elapsedTime +
" nanoseconds to execute");
}
}
}

The @AroundInvoke...