How-To Develop a Stateless Session EJB using EJB 3.0 and JDK 1.4

How-To Develop a Stateless Session EJB using EJB 3.0 and JDK 1.4

Date: 1/18/05
Author: Jason Haley

Introduction

This example application demonstrates Oracle's support for the EJB 3.0 specification in JDK 1.4 environment. Because annotations are only available in JDK 1.5, this will show how to configure many of the feature with an xml-only deployment. It demonstrates:

  • a sparse ejb-jar.xml file
  • how to perform field and property injection of ejb refernces
  • how to perform field and property injection of environment entries
  • how to declare life cycle callbacks

This demonstration uses the WeatherReport bean to demonstrate a simple stateless EJB using EJB 3.0 and JDK 1.4.

Stateless Session Bean example using EJB 3.0 and JDK 1.4

The following is the remote interface for the WeatherReport EJB.

package oracle.ejb30;
public interface WeatherReport {
    public String getTemperatorFor(String cityName);
    public String getTemperatorFor(String cityName, boolean celcius);
}

The bean class is a plain java class that implements a business interface:

package oracle.ejb30;
public class WeatherReportBean implements WeatherReport {
    protected Map weatherMap;
    // both Strings are configured for injection in the ejb-jar.xml file
    private String celciusString;
    private String fahrenheitString;
    private TemperatureConverter temperatureConverter;

    . . .

    public String getTemperatureFor(String cityName) {
        return getTemperatureFor(cityName, false);
    }

    public String getTemperatureFor(String cityName, boolean celcius) {
        . . .
    }

    // this setter is configured for injection in the ejb-jar.xml file
    public void setTemperatureConverter(TemperatureConverter converter) {
        this.temperatureConverter = converter;
    }
}

The following is a callback listener class for the WeatherReport EJB.

package oracle.ejb30;
public interface LifecycleListener {
    public void initializeWeatherData(Object obj) {
        . . .
    }
}

Note that none of the classes make use of annotations, but instead rely on a simplified deployment descriptor.

Here's the content of the simplified deployment descriptor. It denotes the EJB name, bean class, remote/local interface(s), session type, injected references, and the callback listener. The remainder is defaulted.

<ejb-jar>
  <enterprise-beans>
    <session>
      <ejb-name>WeatherReport</ejb-name>
      <remote>oracle.ejb30.WeatherReport</remote>
      <ejb-class>oracle.ejb30.WeatherReportBean</ejb-class>
      <session-type>Stateless</session-type>
      <ejb-local-ref>
        <ejb-ref-name>ejb/TemperatureConverter</ejb-ref-name>
        <local>oracle.ejb30.TemperatureConverter</local>
        <property>temperatureConverter</property>
      </ejb-local-ref>
      <env-entry>
        <env-entry-name>CelsiusString</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>degrees Celsius</env-entry-value>
        <field>celciusString</field>
      </env-entry>
      <env-entry>
        <env-entry-name>FahrenheitString</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>degrees Fahrenheit</env-entry-value>
        <field>fahrenheitString</field>
      </env-entry>
      <lifecycle-callbacks>
        <callback-listener>oracle.ejb30.LifecycleListener</callback-listener>
        <post-construct>initializeWeatherData</post-construct>
      </lifecycle-callbacks>

    </session>
    <session>
      <ejb-name>TemperatureConverter</ejb-name>
      <local>oracle.ejb30.TemperatureConverter</local>
      <ejb-class>oracle.ejb30.TemperatureConverterBean</ejb-class>
      <session-type>Stateless</session-type>
    </session>
  </enterprise-beans>
</ejb-jar>

Things to note:

  • The <field> and <property> tags for reference injection have not been officially defined in the spec and are subject to change
  • The values of the <field> and <property> tags correspond to the fields and setters on the bean that are to be injected
  • The <lifecycle-callbacks> tag and subtags have not been officially defined in the spec and are subject to change
  • If you want to declare the life cycle callbacks on the bean itself, omit the <callback-listener> tag
  • The supported life cycle tags are <post-construct>, <pre-destroy>, <post-activate>, and <pre-passivate> and those used should be listed as subtags of a single <lifecycle-callbacks> tag per bean.
  • an example of configuring interceptors in xml can be found in the interceptor How-To
  • TemperatureConverter shows the minimal amount of xml currently needed to deploy a stateless session bean

Sample EJB Client

Note that the EJB home interface is no longer required, and a bean instance is not created by invoking the create() method. Instead, we can directly invoke a method on the EJB, as shown in the following client accessor:

 

Context context = new InitialContext();
System.out.println("Looking up WeatherReport");
WeatherReport report = (WeatherReport)context.lookup("java:comp/env/ejb/WeatherReport");
System.out.println(weatherReport.getTemperatureFor("Las Vegas"));
System.out.println(weatherReport.getTemperatureFor("Ottawa", true));

Prerequisites

What you need to know

In order to complete the example application, you should be familiar with the following:
  • EJB 3.0

For further information on EJB 3.0, see the following documents on OTN:

Software Requirements

This demonstration requires that the following software components are installed and configured correctly:

Notations

  • %ORACLE_HOME% - The directory where you installed Oracle Application Server EJB 3.0 Preview
  • %JAVA_HOME% - The directory where your JDK is installed
  • %HOWTO_HOME% - The directory where this demo is unzipped

Building the Application

The configuration files are located in the %HOWTO_HOME%/etc directory, including deployment descriptor files such as application.xml.

Running the Application

To run the sample application on a standalone instance of Oracle Application Server EJB 3.0 Preview, follow these steps:

1. Examine the Sample File Directories

  • build - temporary directory created during the build
  • log - temporary directory holding build/deploy logs
  • etc - all necessary files to package the application
  • lib - holds the application archives that could be deployed
  • doc - the How-to document and Javadoc's
    • javadoc - the javadoc of the different source files
    • how-to-ejb30-jdk14.html - this How-to page
  • src - the source of the demo
    • ejb - contains the sample SLSB code
    • client - contains application client code

2. Configure the Environment

Ensure the following environment variables are defined:

  • %ORACLE_HOME% - The directory where you installed OC4J.
  • %JAVA_HOME% - The directory where you installed the JDK

3. Start the Oracle Application Server EJB 3.0 Preview

An Oracle Application Server EJB 3.0 Preview container must be running. Start it using the following command:

    >%ORACLE_HOME%/bin/ejb30 -start

4. Generate, Compile, and Deploy the Application

Ensure Ant 1.6.2 or above is installed on your machine and configured correctly. On some operating systems, Ant does not currently support the use of environment variables. If this is the case for your operating system, please modify the common.xml file located in the %HOWTO_HOME% directory.

Edit ant-oracle.properties (in the demo directory) and ensure the following properties are set to the correct values, as indicated below:

  • oc4j.host: host where OC4J is running (default localhost)
  • oc4j.admin.port: RMI port number (default 23791)
  • oc4j.admin.user: admin user name (default oc4jadmin)
  • oc4j.admin.password: admin user password (default welcome)
  • oc4j.binding.module: website name where deployed web modules are bound (default http-web-site)

To build the application, type the following command from the %HOWTO_HOME% directory:

>ant

You should now have the newly created ejb30-jdk14.ear in your %HOWTO_HOME%/lib directory.

This command would also attempt to deploy the application if the build is successful. It will first test whether OC4J is running.

Note that you can also deploy the application separately . Ensure the %ORACLE_HOME% environment variable is defined, and from the %HOWTO_HOME% directory, type the command:

>ant deploy

5. Run the Application

Run the sample by providing the following command, including a name as the program argument:

>ant run

You will the following output from the Client


[java] Looking up WeatherReport
[java] 60.0 degrees Fahrenheit
[java] 53.0 degrees Fahrenheit
[java] 18.0 degrees Fahrenheit
[java] -7.777777777777778 degrees Celsius
[java] No weather data available for LasVegas

Summary

In this document, you should have learned how to:

  • Develop and deploy a simple stateless session bean using EJB 3.0 and configured entirely using xml
  • Deploy an execute a simple stateless session bean in the Oracle Application Server EJB 3.0 Preview running JDK 1.4

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy