Lab - Deploying a POJO as an EJB3 Stateless Session Bean

Pete Bennett (pbennett@jboss.com)
9 November 2006

This lab will show you how easy it is to take an existing POJO that simply implements your business methods and to deploy it as an EJB3 Stateless Session Bean using annotations.

The POJO Base

The example consists of two very simple classes. org.jboss.tutorial.stateless.bean.Calculator contains a standard Java interface which defines three business methods to perform mathematical operations on two ints. org.jboss.tutorial.stateless.bean.CalculatorBean provides an implementation of this interface that uses println statements to show what it is doing. Note that there is nothing special about these two classes - they are very simple Java.

org.jboss.tutorial.stateless.client.Client is a simple client that can be used to excercise the business methods. It takes a single argument which determines how it should obtain an implementation of the Calculator interface (the -pojo option specifies that the client should create a new local CalculatorBean while the -ejb option specifies that the client should connect to a remote EJB implementation of CalculatorBean). The POJO option works with the code as it stands, this lab will walk you through implementing the EJB option.

Open a command prompt in the src\build directory and run the command "ant runpojo". This compiles the code and runs the client with the POJO option. Here the client uses the constructor to directly create and access a CalculatorBean instance. You can see the interactions printed in the same command prompt by println statements from both the client and the implementation.

Deploying as an EJB3

It is very straightforward to create a Stateless session bean in EJB3. All bean types are homeless in EJB3. That means you only have to create a bean class, have it implement at least one interface, add some annotations to your bean class to indicate to the EJB3 container your intentions and you’ve created a Stateless bean.

The javax.ejb.Stateless annotation marks a class as being a Stateless Session Bean. This annotation does not take any arguments and can be added to a class using the syntax @Stateless just before the class definition in the .java file.

TASK: Import the Stateless annotation class and add the Stateless annotation to CalculatorBean.java.

The javax.ejb.Remote annotation indicates the remote interface that corresponds to the EJB. It takes a single argument which is the class of the remote interface, specificied in brackets using the following pattern: @Remote(RemoteClass.class).

TASK: Import the Remote annotation class and add the Remote annotation to CalculatorBean.java, specifying Calculator as the remote class

Make sure JBoss is running and then open a command prompt in the src\build directory and run "ant ejbjar". This compiles the code and then JARs it into a file called lab-slsb-demo3.jar. If you investigate this file, you will note that it contains no XML config, only the two class files we have just annotated. This command also deploys the file by copying it to the JBoss deploy directory. JBoss scans the file automatically and discovers and deploys our new EJB. If you look at the output from the running JBoss instance you will see confirmation that it has discovered a new EJB in the JAR file and deployed it sucessfully.

Finally, we need to tell the client where to find the EJB we have just deployed. One of the nice features of EJB3 is that it removes a lot of the need for pointless XML configurations by providing sensible defaults. For example, the remote interface for a class FooBar is bound by default to the location FooBar/remote.

TASK: Open Client.java and edit line 40 to point to the correct location in the JNDI tree for the CalculatorBean stateless session bean.

Open a command prompt in the src\build directory and run "ant runejb". This compiles the code and runs the client with the EJB option. Here the client uses a JNDI lookup to bind to the remote interface of the CalculatorBean EJB running in the JBoss AS and then accesses it via the Calculator interface. You can see the interactions printed in the different commands prompt by println statements in both the client (outputs to the ant prompt) and the implementation (outputs to the JBoss prompt).

Conclusion

In this lab we have seen how easy it is to use EJB3 annotations to deploy an existing POJO in a J2EE container and how EJB3's use of sensible defaults coupled with annotations remove much or all of the need for complex, unwieldy XML configuration files.