You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Overview

This example shows how to create a Stateless session EJB using annotations. As stated in the "JSR 220: Enterprise JavaBeansTM,Version 3.0 - EJB Core Contracts and Requirements",

"Stateless session beans are session beans whose instances have no conversational state. This means that all bean instances are equivalent when they are not involved in servicing a client-invoked method. The term 'stateless' signi?es that an instance has no state for a specific client."

With EJB 3.0, it's now possible to write stateless session bean without specifying a deployment descriptor; you basically have to write just a remote or local business interface, which is a plain-old-java-interface, annotated with the @Remote or @Local annotation the stateless session bean implementation, a plain-old-java-object which implements the remote or the local business interface and is annotated with the @Stateless annotation

Download the example via svn:

svn co http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/simple-stateless

To run the example simply type:

$ mvn clean install

The Code

In this example we develop a simple EJB 3 Stateless session EJB. Every stateless session bean implementation must be annotated using the annotation @Stateless or marked that way in the ejb-jar.xml file.

The Stateless bean has 2 business interfaces: CalculatorRemote, a remote business interface, and CalculatorLocal, a local business interface

Local business interface

@Local annotation

Note that it's not mandatory to annotate local business interfaces with the @Local annotation. If a business interface doesn't have any annotation, it's assumed to be local by the ejb container.

Error formatting macro: snippet: java.lang.NullPointerException

You'll notice that in EJB 3.0 the Local Business Interface of a stateless session bean does not need to extend from javax.ejb.EJBLocalObject and does not need a javax.ejb.EJBLocalHome interface. When the bean is looked up from JNDI, the container will create a new reference and pass it to the caller. No home interface required.

Remote business interface
Error formatting macro: snippet: java.lang.NullPointerException

As stated above, the Remote Business Interface of a bean can be any plain old interface. It does not need to extend javax.ejb.EJBObject, it does not need a javax.ejb.EJBHome, the methods do not need to throw javax.rmi.RemoteException, and the bean class can implement it!

Bean
Error formatting macro: snippet: java.lang.NullPointerException

In EJB 3.0 session beans do not need to implement the javax.ejb.SessionBean interface. You can simply annotate it as @Stateless if you want it to be a stateless session bean. And as noted above, notice the bean is actually implement the business interfaces! In the prior version of EJB implementing the remote interface (which derives from javax.ejb.EJBObject) in your bean was just not allowed. Now there is no javax.ejb.EJBObject requirement, so implementing the business interfaces is standard practice for EJB 3.0.

Writing a unit test for the example

Writing an unit test for the stateless session EJb is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods

setUp

Error formatting macro: snippet: java.lang.NullPointerException

Note the "openejb.deployments.classpath.include" parameter, which tells the ejb container to search for EJBs in the classpath, and specifies in which java packages they are to be located.

Test the local business interface

Error formatting macro: snippet: java.lang.NullPointerException

Test the remote business interface

Error formatting macro: snippet: java.lang.NullPointerException

Running

Running the example is fairly simple, just run:

$ cd calculator-stateless-pojo
$ mvn clean install

Which should create output like the following.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.superbiz.calculator.CalculatorTest
Apache OpenEJB 3.0.0-SNAPSHOT    build: 20071214-03:33
http://openejb.apache.org/
INFO - openejb.home = /Users/dblevins/work/openejb3/examples/simple-stateless
INFO - openejb.base = /Users/dblevins/work/openejb3/examples/simple-stateless
WARN - Cannot find the configuration file [conf/openejb.xml].  Will attempt to create one for the beans deployed.
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default JDK 1.3 ProxyFactory)
INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb3/examples/simple-stateless/target/classes
INFO - Configuring app: /Users/dblevins/work/openejb3/examples/simple-stateless/target/classes
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean CalculatorImpl: Container(type=STATELESS, id=Default Stateless Container)
INFO - Loaded Module: /Users/dblevins/work/openejb3/examples/simple-stateless/target/classes
INFO - Assembling app: /Users/dblevins/work/openejb3/examples/simple-stateless/target/classes
INFO - Jndi(name=CalculatorImplLocal) --> Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=CalculatorImplRemote) --> Ejb(deployment-id=CalculatorImpl)
INFO - Created Ejb(deployment-id=CalculatorImpl, ejb-name=CalculatorImpl, container=Default Stateless Container)
INFO - Deployed Application(path=/Users/dblevins/work/openejb3/examples/simple-stateless/target/classes)
INFO - OpenEJB ready.
OpenEJB ready.
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.721 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

  • No labels