Sample of a Stateless Session Bean in EJB 3.0
This sample will demonstrate the following new features from EJB 3.0
- Elimination of the requirement for EJB component interfaces for session beans. The required business interface for a session bean can be a plain Java interface rather than an EJBObject, EJBLocalObject, or java.rmi.Remote interface.
- Elimination of the requirement for home interfaces for session beans.
- Encapsulation of environmental dependencies and JNDI access through the use of annotations, dependency injection mechanisms, and simple lookup mechanisms.
- Introduction of Java metadata annotations to be used as an alternative to deployment descriptors.
Calculator.java: A stateless session bean that implements a simple java interface instead of an EJB component interface like EJBObject, EJBLocalObject or java.rmi.Remote. By annotating this class as a @Stateless session there is no need for a deployment descriptor to describe it separately. This class implements both a local and remote business interface, namely CalculatorLocal and CalculatorRemote.
CalculatorLocal.java: Since this is a local business interface, it is optional that the coder marks this class with a @Local annotation. A business interface which is not annotated with @Local or @Remote is assumed to be Local.
CalculatorRemote.java: Since this is a remote business interface, it must be annotated with the @Remote annotation.
CalculatorServlet.java: This is a servlet to process the form on the jsp page. It uses the stateless session bean Calculator to do some computation and returns the result. Note that CalculatorLocal is being annotated with the @EJB annotation. The ejb container will route every request to different bean instances. Note: a stateful session bean must be declared at the type level, whereas a stateless session bean may be declared at any level.
The structure of the deployable should look like the following:
application.xml: The JAR file is referenced to provide the functionality of this deployable. The WAR file is referenced in order to show the usage of this deployable through a web based interface. The context-root is set to be /calculator so that the URL for this application will be http://<hostname>:<port>/calculator. This is generated by maven during the build and may be found in the ear or at calculator-stateless-ear/target/application.xml. The versions will vary depending on the source code version.
plan.xml: A Geronimo plan is not needed for standalone deployment of this application but is recommended to fix the module id. This example is completed by the car-maven-plugin in calculator-stateless-jetty. The dependencies are not actually needed for standalone deployment.