Interceptor on @Timeout method

Moderator: admin

Post Reply
IndoKnight
Posts: 9
Joined: Thu Feb 27, 2014 11:47 am
Contact:

Interceptor on @Timeout method

Post by IndoKnight »

Hi,
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,
@Interceptors({PrincipalLogger.class}) interceptor is not called on @Timeout method
@Timeout
public void timerSignal(){
System.out.println("Timer time-out");
}
Below is my code which works,

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!");		
	}

}
CalculatorLoggingInterceptor.java

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;
	}
}

Sample output -

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
I'm re-using the method log() as business method interceptor and @timeout method interceptor methods. Also, note that annotating backgroundProcessing() as @Schedule, also works fine!


Cheers,
IndoKnight

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Interceptor on @Timeout method

Post by admin »

As per Section 12.4 of EJB 3.1 Specification:
Interceptor methods may be defined for EJB timer timeout methods of session beans and message-driven beans. These are referred to as AroundTimeout methods.
Within an AroundTimeout method, the InvocationContext.getTimer() method returns the javax.ejb.Timer object associated with the timeout being intercepted.
So it should work. Can't really comment on Frits' document. You might want to post this on JavaRanch and see if he replies.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

IndoKnight
Posts: 9
Joined: Thu Feb 27, 2014 11:47 am
Contact:

Re: Interceptor on @Timeout method

Post by IndoKnight »

Thanks Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests