A Look at EJB 3.1
With the introduction of JEE6, EJB 3.1 brought several improvements over EJB 3.0. With EJB 3.1 there is now no need to define home/local interfaces. Now an EJB can be defined simply with an annotation. For example to define a stateless session bean:
@Stateless
public class SimpleSessionBean {…}
Singleton Beans are introduced with version 3.1. The application server guarantees that there is a single instance of Singleton EJB which can be used for shared data at the application level.
Asynchronous EJB calls are now supported with the @Asynchronous method-level annotation. It is enough to annotate the method with this annotation to make it asynchronous.
The use of timer services have also been simplified with version 3.1:
@Schedule(second = "*/10", minute = "*", hour = "*", persistent = false)
public void doWork() {
System.out.println("timer fired");
}
This method will be called every 10 seconds by the container. The @Schedule annotation is used for setting a timer to be fired based on a cron-like time expression.
To deploy an EJB, the EJB is packaged into a jar (it may also be packaged into a war) and the jar should be copied to $GLASSFISH_HOME/domains/domain1/autodeploy and the application server will handle the rest.
To use an EJB in another server-side component we can use dependency injection capabilities of the container with the @EJB annotation:
@EJB SimpleSessionBean beanRef;
It is also possible to call an EJB from a standalone java client (outside a container):
Context ctx = new InitialContext();
//glassfish v3 uses a no-arg constructor in contrast to
//old way of creating an initial context with jndi properties
SimpleSessionBeanRemote mySessionBean = (SimpleSessionBeanRemote)
ctx.lookup("java:global/TestEjb/SimpleSessionBean");
// obtain a remote reference
The classpath of the client should include the EJB jar file and $GLASSFISH_HOME/modules/gf-client.jar file. For the global JNDI naming of the EJB modules please see here.
The stateless session bean with a timer method example together with a standalone java test client can be downloaded from here
About this entry
You’re currently reading “ A Look at EJB 3.1 ,” an entry on Sirius ICT
- Published:
- 2.26.10 / 7pm
- Category:
- Blog, Development Issues, Thougths and Reflections
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]