Thursday, February 16, 2012

Timers and Interceptors in EJB3

Interceptors are used to intercept business method invocations og EJB3 bean class. The interceptors can be defined either class level or method level.
Also the interceptors can be in a seperate class using @Interceptor annotation or a method annotated with @AroundInvoke with signature Object methodname (InvocationContext ic)in the same class.
More than one interceptor classes can also be defined for a bean, using @Interceptor annotation. Interceptors have same life cycle as of that of associated bean and can have dependency injection in the same context.
Invocation Context
InvocationContext allows you to propagate through a chain of interceptors, along with its get/set methods, which even provides reference to the bean. Following are the methods that can be accessed by the handle provided.
Object getBean(): Provides the reference to the bean for which the Interceptor is defined.
Method getMethod():Provides information of about which method in the bean is called
Object[] getParameters():Provides infomation of the parameters passed to the bean.
void setParameters(): Set the parameter values
Map getContextData(): Provides context data that can be shared by the chain.
Object proceed(): Proceeds to the next interceptor in the chain or to the business method of the bean.
A simple example of using the Interceptor is illustrated below
package com.test;


1.import javax.ejb.Remote;
2.import javax.ejb.SessionContext;
3.import javax.ejb.Stateless;
4.import javax.ejb.TimedObject;
5.import javax.ejb.Timeout;
6.import javax.ejb.Timer;
7.import javax.ejb.TimerHandle;
8.import javax.ejb.TimerService;
9.@Stateless
mailto:10.@RemoteBinding(jndiBinding=%22Welcome")
11.@Remote(UpdateEJB.class)
12.@Interceptors(TestInterceptor.class)
13.public class UpdateEJBBean implements UpdateEJB {
14.private String message;
15.@Resource
16.private SessionContext ctx;
17.@Interceptors(TestInterceptor1.class)
18.public String sayHello(String name) {
19.System.out.println("Inside EJB method");
20.return "Hello " + name + message;
21.}
22.@AroundInvoke
23.public Object log(InvocationContext invocationContext) throws Exception {
24.System.err.println(invocationContext.getMethod().getName() + " called from interceptor 3");
25.return invocationContext.proceed();
26.}
27.}
28.The @RemoteBinding(jndiBinding="Welcome") is used to bind the EJB Bean to the JNDI name which we specify, instead using default JNDI name, provided by appserver vendors
29.The interceptor class will be like the one shown below.
30.public class TestInterceptor{
31.@AroundInvoke
32.public Object log(InvocationContext invocationContext) throws Exception {
33.System.err.println(invocationContext.getMethod().getName() + " called from interceptor 1");
34.return invocationContext.proceed();
35.}
36.}

1.public class TestInterceptor1{
2.@AroundInvoke
3.public Object log(InvocationContext invocationContext) throws Exception {
4.System.err.println(invocationContext.getMethod().getName() + " called from interceptor 1");
5.return invocationContext.proceed();
6.}
7.}
The order of Interceptor Invocation will be like
1.EJB Bean Class level Interceptor
2.EJB Bean Method level interceptor annotated by @Interceptor above the method
3.The method annotated with @AroundInvoke annotation.
Timer Service
Using EJB3 we can create a timely triggers and method invocations using the TimerService, provided by EJB3. You can either implement TimedObject interface, or you can
annotate method that you need to trigger after each timer expiration using @Timeout annotation.

1.package com.test;
2.import javax.ejb.Remote;
3.import javax.ejb.SessionContext;
4.import javax.ejb.Stateless;
5.import javax.ejb.TimedObject;
6.import javax.ejb.Timeout;
7.import javax.ejb.Timer;
8.import javax.ejb.TimerHandle;
9.import javax.ejb.TimerService;
10.import javax.ejb.TransactionAttribute;
11.import javax.ejb.TransactionManagement;
12.import javax.interceptor.AroundInvoke;
13.import javax.interceptor.InvocationContext;
14.@Stateless
mailto:15.@RemoteBinding(jndiBinding=%22Welcome")
16.@Remote(UpdateEJB.class)
17.public class UpdateEJBBean implements UpdateEJB,TimedObject {
18.private String message;
19.@Resource
20.private SessionContext ctx;
21.public String sayHello(String name) {
22.ctx.getTimerService().createTimer(6000, "HAI");
23.ctx.getTimerService().createTimer(6000,6000, "HAI");
24.return "Hello " + name + message;
25.}
26.public void ejbTimeout(Timer handle) {
27.System.out.println("handle.getTimeRemaining()"+handle.getTimeRemaining());
28.System.out.println("handle.getNextTimeout()"+handle.getNextTimeout());
29.}
30.}
To create the timer ctx.getTimerService().createTimer method is used, the timer timeout can be only once or frequently in a timed manner.
createTimer(6000,6000, "HAI") this says that the timer will expire in a period of 6000ms with initial delay of 6000ms.
The argument, Timer provides the handle to the timer, which has getTimeRemaining and getNextTimeout methods.
Instead of implementing interface you can also use annotation @Timeout above the method that needs to be triggered after each timer expiration.

</div>