Stateless Beans

It is very easy to create a Stateless Bean with EJB 3.0. All bean types are homeless in EJB 3.0 so all you have to do to create a Stateless bean is to create a bean class and have it implement at least one interface. Take a look at CalculatorBean.java

ejb-jar.xml

CalculatorBean is defined as a stateless session bean through the <session> and <session-type> elements. This marks the class as a stateless bean and the deployer will deploy that class as a stateless bean EJB container.

CalculatorBean also implements two interfaces. One is the remote interface of the EJB the other is the local interface.

Take a look at CalculatorRemote.java. To define this as the remote interface of Calculator bean you specify the interface with the <remote> tag. Similar for CalculatorLocal.java as you need to specify the local interface with the <local> tag.

XML Validation

Notice that schemas are referenced in ejb-jar.xml and jboss.xml. In the former, the URL is http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd. In the latter, the URL is http://www.jboss.org/j2ee/schema/jboss_5_0.xsd. We recommend that you include these schemas in your deployment descriptors so that on deployment, the xml can be validated. If the schemas are provided, a deployment exception will occur if either of the two descriptors are invalid. If the schemas are not included, there is no validation on deployment and debugging can be more difficult. Note that these schema files are also provided locally in the jboss-ejb3.jar file so that validation can occur off-line.

JNDI Bindings

The Calculator bean will have two JNDI bindings for the remote and Local interface. By default, JBoss will use ejbName/local and ejbName/remote for the local and remote interfaces, respectively.

Client

Open up Client.java. You'll see that it looks up the stateless bean under "ejbName/remote". Also notice that there is no Home interface and you can begin executing on the stateless bean right away.

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

run:
     [java] 2004-10-06 19:10:35,857 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo
rt: org/apache/axis/AxisFault
     [java] 1 + 1 = 2
     [java] 1 - 1 = 0

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 .jar. 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 CalculatorBean 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.