Getting Started with Enterprise JavaBeans™

G

Session Beans

Tutorial Home Section Home Previous Next

Get

Get

Example: A Metric Conversion Program

     Our first example uses a stateless session bean and a Web component to provide an application that performs various conversions between U.S. English and metric measures. It might come in handy for a salesman working for a U.S. company in Europe or similar scenarios. For a look at the display and user view of the application, see The Metric Converter Application. You should also read about and deploy the Alice's World page in order for everything to work properly; details are at Appendix C: About the Example Applications.

     Stateless session beans do not have much code to interact with the container. In this example, you will see that the SessionBean implementation is all empty methods. As to the functional part of MetricCvtBean, there is a convert() method that takes a String argument, to identify the type of conversion desired, and a double argument, which is the value to convert. All of the other methods perform the actual conversions from and to the selected measures. From an EJB perspective, the most important thing to notice about the code is that the class implements the SessionBean interface. Here's an abbreviated look at the MetricCvtBean code:


import javax.ejb.*;

public class MetricCvtBean implements SessionBean, 
                                      MetricCvtConstants
{ 
  public MetricCvtBean() {} // end constructor

// required by the specification contract
  public void ejbCreate() {} // end ejbCreate

// required for SessionBean implementation
  public void ejbRemove() {}
  public void ejbActivate() {}
  public void ejbPassivate() {}
  public void setSessionContext(SessionContext sc) {}

  public String Convert( String sType, double dToConvert )
  {
    ...
  } // end Convert

  // other methods
  ...

 } // end class MetricCvtBean

     Now that we have the main bean code, it's time to create the home and component interfaces. The Metric Converter Web component is written to remote interfaces. Here's the abbreviated remote component interface code:


import java.rmi.RemoteException;
import javax.ejb.*;


public interface MetricCvtRemote extends EJBObject, 
                                         MetricCvtConstants
{
  public String Convert( String sType, double dToConvert ) 
                throws RemoteException;

// other method declarations
  ...

} // end MetricCvtRemote

     The important features of the remote component interface are that it extends EJBObject and that every method is declared to throw a RemoteException. In contrast, a local component interface extends EJBLocalObject and does not throw RemoteExceptions; the code looks very much like a standard Java interface.

     Next, we'll create the remote home interface:


import javax.ejb.*;
import java.rmi.RemoteException;

public interface MetricCvtRemoteHome extends EJBHome 
{
  // required
  MetricCvtRemote create() throws RemoteException, 
                                  CreateException;
} // end MetricCvtRemoteHome

     The important things here are that the interface extends EJBHome, that the create() method returns the remote component interface, and that the method throws RemoteException and CreateException. A local home interface would extend EJBLocalHome, its create() method would return the local component interface, and the method would throw only CreateException.

     Now it's time to compile everything. You'll notice that the J2EE documentation recommends using Ant. You will be well rewarded for learning the tool, but the tutorial can only cover so much territory; we will rely instead on the familiar and standard javac command (in addition, there's always a benefit in knowing how to do things from scratch). Go to the MetricCvt directory, and from the command line enter

javac *.java


to compile all Java files in the directory. If you prefer, you can copy the .class files from the MetricCvt/prod directory.

     Next, we want to take a quick look at the client code (the client is a JSP) to access and use the bean. Following are the relevant portions:


  private MetricCvtRemote mc = null;

  public void jspInit() 
  { 
    InitialContext ic;
    MetricCvtRemoteHome mcHome;
    Object oRef;
    String sMsg = "Couldn't create MetricCvtBean.";
  
    try 
    {
      ic = new InitialContext();
      oRef = 
        ic.lookup( "java:comp/env/ejb/MetricCvtBean" );
      mcHome = 
        (MetricCvtRemoteHome)PortableRemoteObject.
            narrow( oRef, MetricCvtRemoteHome.class );
      mc = mcHome.create(); 
    } 
    catch( RemoteException re ) 
    {
      System.out.println( sMsg + 
         re.getMessage() );
    } 
    catch( CreateException ce ) 
    {
      System.out.println( sMsg + 
         ce.getMessage() );
    } 
    catch( NamingException ne ) 
    {
      System.out.println( "Unable to lookup home: " + 
      "MetricCvtBean. " + ne.getMessage() );
    } 
  } // end jspInit

  public void jspDestroy() 
  { 
    try 
    {
      mc.remove();
    } 
    catch( RemoteException remoteEx ) 
    { 
      // don't care 
    } 
    catch( RemoveException removeEx ) 
    { 
      // don't care 
    } 

    mc = null; 
  } // end jspDestroy

     In jspInit(), the code creates an InitialContext and then performs a JNDI lookup for MetricCvtBean. It then retrieves the home interface and uses the create() method to get a reference to the component interface. In jspDestroy(), the bean is removed and the reference is set to null.

     A client uses the retrieved component interface to invoke exposed bean methods. The actual use of the bean in the Metric Converter is to display the output from the Convert() method, as shown by the following statement:


	<%= mc.Convert( sRB, d ) %>


Tutorial Home Section Home Previous Next