JBoss.org Community Documentation

Chapter 31. Configuring Stateful Session Beans with deployment descriptors in EJB3

Take a look at the META-INF/ejb-jar.xml and org.jboss.tutorial.stateful_deployment_descriptor.bean.ShoppingCartBean. You specify a stateful bean with the "session" and "session-type" tags. Note that all bean types in EJB 3.0 are homeless, so there is no requirement for a "home" or "local-home" tag. The bean class is specified with the "ejb-class" tag.

ShoppingCartBean also implements a business-remote interface. Take a look at org.jboss.tutorial.stateful_deployment_descriptor.bean.ShoppingCart. To define this as the business-remote interface of ShoppingCartBean you need to use the "business-remote" tag in the ejb-jar.xml. Here's the META-INF/ejb-jar.xml:



There's a very important difference between the remote and a business-remote interface. The EJB2.x remote interfaces, which extend from EJBObject, are referred through the <remote> tag in the ejb-jar.xml. On the other hand, the EJB3 style Plain Old Java Interface which is implemented by your EJB3 style POJO bean is known as the business-remote interface and is represented by the @Remote and it's corresponding <business-remote> tag in ejb-jar.xml. Similar is the case with <local> and the <business-local> tags in ejb-jar.xml.

@Remove :

Take another look at META-INF/ejb-jar.xml. Look for the "remove-method" tag. Instead of explicitly calling EJBObject.remove() in your applications and thus polluting it further with J2EE specific code, any method specified in the "remove-method" tag will cause the stateful bean instance to be removed from the container at the end of the method call. This deployment descriptor behavior mimics the @Remove annotation.

JNDI Bindings through deployment descriptor :

The CalculatorBean will have its remote interface bound in JNDI. Take a look at META-INF/jboss.xml. Note the jndi-name tag. This specifies the jndi binding for the remote interface of the bean.

Client :

Open up org.jboss.tutorial.stateful_deployment_descriptor.client.Client. You'll see that it looks up the stateful bean under its jndi name. 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.

Building and Running

From the command prompt, move to the "stateful_deployment_descriptor" folder under the Section 1.3, “Set the EJB3_TUTORIAL_HOME”

Ant Users:

Make sure your JBossAS-5.x is running

$ ant
$ ant run

     [java] Buying 1 memory stick
     [java] Buying another memory stick
     [java] Buying a laptop
     [java] Print cart:
     [java] 2     Memory stick
     [java] 1     Laptop
     [java] Checkout
     [java] Should throw an object not found exception by invoking on cart after @Remove method
     [java] Successfully caught no such object exception.


Maven Users: Make sure the AS is not running.

$ mvn clean install -PRunSingleTutorial