Enterprise JavaBeans

Reuven introduces an infrastructure for creating distributedapplications using JBoss, the Java application server.
Writing the Classes

Now that we understand which classes we have to create, we can begin to write some code. You'll quickly notice that there is not that much code to write, and that in the case of our CalculatorBean class, many of our methods are defined with empty bodies. This is because the SessionBean interface, from which CalculatorBean inherits, forces us to define these methods, even if our bean is simple enough not to use them. Using empty method bodies fulfills our obligations to the interface, while keeping our class simple.

I put all of the Java source files in the il.co.lerner.calculator package, reflecting the fact that they come from my commercial domain and that this is the calculator project. As such, all of the .java source files are in a directory hierarchy il/co/lerner/calculator.

Our bean class, CalculatorBean (see Listing 2), defines a single multiply() method, which takes two integer inputs and returns an integer to its caller. Other than implementing the SessionBean interface, CalculatorBean really doesn't have much to do with EJB; indeed, it is a fairly boring class with a single method. Anything we write to System.out will be printed to the JBoss session log.

Listing 2. CalculatorBean.java, the Bean Class for Our EJB Calculator

Our home interface, CalculatorHome, allows us to create a new instance of CalculatorBean. Other than defining the interface's signature, including the fact that it returns an instance of the remote interface (Calculator), the home interface is extremely short:

package il.co.lerner.calculator;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface CalculatorHome extends EJBHome
{
   Calculator create() throws RemoteException,
   CreateException;
}

Finally, our remote interface, Calculator, lists one method signature for each public method in CalculatorBean:

package il.co.lerner.calculator;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Calculator extends EJBObject
{
    public int multiply(int num1, int num2) throws
    RemoteException;
}
Client programs will be invoking methods via the remote interface, rather than directly on the bean. The signatures for the remote interface and the bean class must match, or you will encounter serious problems later on.

Deploying the Bean

Now that we have defined them, we can deploy our Calculator session bean to our running JBoss server. Deploying our session bean means taking all of its elements and turning them into a single Java archive (jar) file. Our .jar file will contain the compiled classes for Calculator, CalculatorHome and CalculatorBean.

But, it will also contain a “deployment descriptor”, an XML file named ejb-jar.xml that describes the contents of the .jar file to the EJB container. Deployment descriptors are a mandatory part of the EJB standard and do not vary from one application server to another. They tell the EJB container the names of the interfaces and classes that we have chosen, and also allow us to define such items as the type of transactions our bean will support. The deployment descriptor for our Calculator EJB is in Listing 3 and should be placed in the same directory as the .java source files.

Listing 3. ejb-jar.xml, the Deployment Descriptor for Our Calculator Bean

Our .jar file will also contain a short XML file named jboss.xml, which we will place alongside ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss>
  <enterprise-beans>
    <session>
      <ejb-name>Calculator</ejb-name>
      <jndi-name>calculator/Calculator</jndi-name>
    </session>
  </enterprise-beans>
</jboss>

The jboss.xml file is specific to JBoss, binding our bean to the Java's Naming and Directory Interface (JNDI). With jboss.xml in place, a client program that asks JNDI for calculator/Calculator will get a reference to it in return.

We could build the .jar file by hand, but it's easier to use Ant to build the .jar file and deploy it into the right place. Listing 1 [ftp.linuxjournal.com/pub/lj/listings/issue92/5497.tgz] contains an Ant build.xml that supports the targets ejb-jar (the default) and deploy. If you place build.xml in $CALCULATOR, then your .java files, ejb-jar.xml and jboss.xml should be in $CALCULATOR/il/co/lerner/calculator. Ant will place the results of compilation in $CALCULATOR/build/calculator, as specified in the build.calculator.dir property in build.xml.

With Ant installed in $ANT, we can compile our .java files, turn them into an EJB-compliant .jar file (with the ejb-jar.xml file in the mandatory META-INF directory) and deploy it to JBoss with the following command:

$ANT/bin/ant deploy

You should see a number of messages on the screen describing the compilation and deployment process. If the compilation or build fails, check that your environment variables are set correctly, that the Java files don't have any syntax errors and that the directories have appropriate permissions.

If your JBoss server already is running before you deploy the Calculator .jar file, you will notice that it automatically detects and deploys the file without any need for restart. This is one of the great pleasures of JBoss; in order to deploy your EJB .jar file, you simply copy it into the $JBOSS_DIST/deploy directory.

______________________

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState