Timer Service and Callbacks
This example shows you how to access javax.ejb.SessionContext as well as using the EJB Timer Service. It also explains how
callbacks work in EJB 3.0.
SessionContext injection
The javax.ejb.SessionContext is injected using the @Inject annotation. When the stateless bean instance is created
the field will be initialized with the correct SessionContext. Take a look at ExampleTimerBean.java
private @Inject SessionContext ctx;
Callbacks
The rest of the bean example registers a timer with the EJB Timer service. In the EJB 2.1 specification it was required to implement an interface to get ejbTimeout callbacks. This is not needed in EJB 3.0 as all you have to define is a method annotated with javax.ejb.Timeout. This is the same with other callbacks like ejbPassivate, ejbRemove, etc... No interface is needed to be implemented, just declare the methods as you need them.
Building and Running
To build and run the example, make sure you have ejb3.deployer installed in JBoss 4.0.x and have JBoss running. See the reference manual on how to install EJB 3.0.
Unix: $ export JBOSS_HOME=<where your jboss 4.0 distribution is> Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is> $ ant $ ant run
After 5 seconds look at the JBoss console window to see the Timer being fired.
00:51:20,074 INFO [STDOUT] --------------------- 00:51:20,074 INFO [STDOUT] * Received Timer event: Hello World 00:51:20,074 INFO [STDOUT] ---------------------
Comments