JBoss.org Community Documentation
It is very easy to create a Stateful Bean with EJB 3.0. All bean types are homeless in EJB 3.0 so all you have to do to create a
Stateful bean is to create a bean class and have it implement at least one interface.
Take a look at org.jboss.tutorial.stateful.bean.ShoppingCartBean
in the tutorial.
The first thing to notice is that the class is tagged as @Stateful
. This marks the class as a stateful bean and
the deployer will deploy that class as a stateful bean EJB container.
ShoppingCartBean also implements a remote interface. Take a look at org.jboss.tutorial.stateful.bean.ShoppingCart
Take another look at org.jboss.tutorial.stateful.bean.ShoppingCartBean
.
Look for the method annotated as @Remove. Instead of explicitly calling EJBObject.remove() in your applications and thus polluting it
further with J2EE specific code, any method tagged with @Remove will cause the stateful bean instance to be removed from the container
at the end of the method call.
The ShoppingCartBean will have its remote interface bound in JNDI, by default, under the ejbName/local and/or ejbName/remote for the local and remote interfaces, respectively.
Open up org.jboss.tutorial.stateful.client.Client
.
You'll see that it looks up the stateful bean under "ejbName/remote". Also notice that there is no Home interface and you can begin
executing on the stateful bean right away. When you access the bean in JNDI, an instance of the stateful bean will be created on the server.
So, when you need a different instance of the stateful bean, you do an additional jndi.lookup() to get this new reference.
To build and run the example, make sure you have installed JBoss 5.x. See the Section 1.1, “JBoss Application Server 5.x” for details.
From the command prompt, move to the "stateful" folder under the Section 1.3, “Set the EJB3_TUTORIAL_HOME”
Make sure your JBossAS-5.x is running
$ ant $ ant run
$ mvn clean install -PRunSingleTutorial