Overview
This example shows how to create a session stateful EJB using annotations.
A stateful session bean is a session bean whose instances can maintain the conversational state with the client. The conversational state of the stateful session bean, which describes the conversation between a speci?c client and a session bean, is contained in the fields of the stateful session bean.
With EJB 3.0, it's now possible to write stateful 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 stateful session bean implementation, a plain-old-java-object which implements the remote or the local business interface and is annotated with the @Stateful annotation
Download the example via svn:
svn co http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/simple-stateful
To run the example simply type:
$ mvn clean install
The Code
In this example we develop a simple counter stateful session EJB.
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.
Remote business interface
Bean
Writing a unit test for the example
Writing an unit test for the stateful 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
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
Test the remote business interface
Running
Running the example is fairly simple, just run:
$ cd simple-stateful
$ mvn clean install
Which should create output like the following.
------------------------------------------------------- T E S T S ------------------------------------------------------- [OPENEJB:surefire] Running org.apache.openejb.examples.counter.CounterImplTest log4j:WARN No appenders could be found for logger (OpenEJB). log4j:WARN Please initialize the log4j system properly. Apache OpenEJB 3.0-incubating-SNAPSHOT build: 20070105-12:45 http://incubator.apache.org/openejb OpenEJB ready. [OPENEJB:surefire] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 1.943 sec [OPENEJB:INFO] [jar:jar] [OPENEJB:INFO] Building jar: openejb3/examples/simple-stateful/target/simple-counter-statefuls-1.0-SNAPSHOT.jar [OPENEJB:INFO] [install:install] [OPENEJB:INFO] Installing openejb3/examples/simple-stateful/target/simple-counter-statefuls-1.0-SNAPSHOT.jar ... [OPENEJB:INFO] ------------------------------------------------------------------------ [OPENEJB:INFO] BUILD SUCCESSFUL [OPENEJB:INFO] ------------------------------------------------------------------------