Version 4

    Stateful Beans

    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


    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  To define this as the remote interface of ShoppingCartBean

    you only need to tag it as @Remote.



    Take another look at  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. 


    JNDI Bindings

    The ShoppingCartBean will have its remote interface bound in JNDI, by default, under the fully qualified class name of the remote interface.  This makes it very easy to reference the JNDI name in lookups as you can just do ShoppingCart.class.getName()



    Open up  You'll see that it looks up the stateful bean under its remote interface's fully qualified classname.  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

    To build and run the example, make sure you have ejb3.deployer installed in JBoss 4.0.x and have JBoss running.  See the reference manual on how to install EJB 3.0. 

    Unix:    $ export JBOSS_HOME=<where your jboss 4.0 distribution is>
    Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is>
    $ ant
    $ ant run
         [java] Buying 1 memory stick
         [java] 2004-10-06 19:37:16,869 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo
    rt: org/apache/axis/AxisFault
         [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.


    The INFO message you can ignore.  It will be fixed in later releases of JBoss 4.0.


    Jar structure

    EJB 3.0 beans must be packaged in a JAR file with the suffix .ejb3.  Running the ant script above creates a JAR file within the deploy/ directory of JBoss.  All that needs to be in that jar is your server-side class files.  So basically just the ShoppingCartBean and the interfaces it implements.  JBoss will automatically browse the JAR file to determine if any EJBs are annotated by any classes within it.  There is no precompilation step.