I have something demonstrated on my system using JBoss7.1.1 application server, where I used EJB3.1 stateless session bean with @Timeout method with interceptor and it works! This contradicts to Frits Enterprise Java Beans 3.pdf document. Correct me if I'm wrong in the interpretation of the document.
In the document on page 42 it says,
Below is my code which works,@Interceptors({PrincipalLogger.class}) interceptor is not called on @Timeout method
@Timeout
public void timerSignal(){
System.out.println("Timer time-out");
}
CalculatorBean.java (for brevity I have not added CalculatorLocal and CalculatorRemote classes)
Code: Select all
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.TimerService;
import javax.interceptor.Interceptors;
@Stateless
@LocalBean
@Local(CalculatorLocal.class)
@Remote(CalculatorRemote.class)
public class CalculatorBean {
@Resource
private int intNumEntry;
@Resource
private TimerService timerService;
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
@Interceptors({ CalculatorLoggingInterceptor.class })
public Object getEnvEntry() {
if(timerService.getTimers().size()==0){
timerService.createTimer(10000, "Refresh the url and you see me!");
}
return intNumEntry;
}
@Interceptors({ CalculatorLoggingInterceptor.class })
@Timeout
public void backgroundProcessing() {
System.out.println("Hi, IndoKnight!");
}
}
Code: Select all
import javax.interceptor.AroundInvoke;
import javax.interceptor.AroundTimeout;
import javax.interceptor.InvocationContext;
public class CalculatorLoggingInterceptor{
@AroundTimeout
@AroundInvoke
public Object log(InvocationContext ctx) throws Exception{
System.out.println("In log(InvocationContext ctx) for method : "+ctx.getMethod().getName());
Object returnObj = ctx.proceed();
System.out.println("Exiting log(InvocationContext ctx) for method : "+ctx.getMethod().getName());
return returnObj;
}
}
Code: Select all
19:10:38,191 INFO [stdout] (EJB default - 6) In log(InvocationContext ctx) for method : backgroundProcessing
19:10:38,192 INFO [stdout] (EJB default - 6) Hi, IndoKnight!
19:10:38,192 INFO [stdout] (EJB default - 6) Exiting log(InvocationContext ctx) for method : backgroundProcessing
Cheers,
IndoKnight