How-To Develop EJB 3.0 to Supports Backward Compatibility with EJB 2.x Clients

How-To Develop EJB 3.0 to Supports Backward Compatibility with EJB 2.x Clients

Date: 6/12/06
Author: Debu Panda

Introduction

This demo application showcases the support Oracle Application Server 10g 10.1.3.1 has for the EJB 3.0 specification, demonstrating support for the optional use of EJBHome, and how to migrate EJB components to EJB 3.0 without breaking the old EJB 2.x clients. This example utilizes a Stateful EJB.

EJB 3.0 greatly simplifies development of EJB applications by removing a lot of the complexities developers faced in preview versions. Creating a simple stateful EJB using EJB 2.1 requires a bean class, two interfaces and a deployment descriptor. The component interface (remote and/or local) and home interfaces have to extend javax.ejb.EJBObject and javax.ejb.EJBHome respectively and the bean class has to implement the javax.ejb.SessionBean interface.

With EJB 3.0, many of these requirements have been relaxed:

  • The bean class may be a plain java class (POJO)
  • The EJB interface may be a pure java interface (POJI)
  • You no longer must provide an EJBHome interface
  • Annotations may be used instead of an XML deployment descriptor

We will create a basic shopping cart EJB to demonstrate a simple stateful EJB using EJB 3.0. This example will have an optional home interface.

Stateful Session Bean using EJB 3.0 

Here is the remote interface for Cart EJB.

Note that this is a pure Java interface and it does not extend  EJBObject.   

package oracle.ejb30;
import java.util.Collection;
import javax.ejb.Remote;
@Remote
public interface Cart {
	public void addItem(String item);
	public void removeItem(String item);
	public Collection getItems();
}

>Here is the code for the bean class. The EJB is a plain Java class that implements its business interface.

package oracle.ejb30;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Init;
import javax.ejb.Stateful;

@Stateful
@RemoteHome(CartHome.class)
public class CartBean implements Cart {
private ArrayList items;

@Init
public void initialize() {
		items = new ArrayList();
	}

public void addItem(String item) {
		items.add(item);
	}
public void removeItem(String item) {
		items.remove(item);
	}
public Collection getItems() {
		return items;
	}
}

Any method annotated with @Init annotation is invoked when an EJB 2.1 client invokes a create method on stateful session bean instance. The @RemoteHome(CartHome.class) annotation marks the home interface for the bean to be oracle.ejb30.CartHome.

The EJB Client

EJB client code is dramatically simplified in EJB 3.0. The client no longer requires the retrieval of the EJB home interface from JNDI, and does not have to specifically create a bean instance by invoking create on the home interface. Clients can now simply and directly invoke a method on the EJB.

However with EJB being utilized in distributed systems, the luxury of changing deployed client code may not be afforded to all applications. Therefore it may be required to support existing client code bases which use the old style lookup based on the retrieval of the EJBHome object and the calling of the create method to obtain an instance of the bean.

 Here is a snippet from client code that accesses the EJB

public class CartClient {
public static void main(String[] args) throws Exception {
Context context = new InitialContext();
System.out.println("Looking up Cart");
CartHome cartHome = 

Cart cart = cartHome.create();


System.out.println("Adding items to cart");
cart.addItem("Item1");
cart.addItem("Item2");
System.out.println("Listing cart contents");
Collection items = cart.getItems();
for (Iterator i = items.iterator(); i.hasNext();) {
String item = (String) i.next();
System.out.println("  " + item);
}
}
}

Prerequisites

What you need to know

  • You should be familiar with these technologies: EJB in general and some insight into the EJB 3.0 specification
  • Link to other EJB 3.0 Howto's on OTN

Software Requirements

This demo requires that the following sofware components are installed and configured correctly:

Notations

  • %ORACLE_HOME% - The directory where you installed Oracle Application Server 10g 10.1.3
  • %JAVA_HOME% - The directory where your JDK is installed
  • %HOWTO_HOME% - The directory where this demo is unzipped

Building the Application

The important configuration files are all under %HOWTO_HOME%/etc directory, deployment descriptors files like application.xml,, etc.

Run the Application

The following instructions are for running this demonstration on a standalone instance of Oracle Application Server 10g 10.1.3.1

Examining the How to Distribution

  • 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-compatibility-ejb2x.html - this How-to page
  • src - the source of the demo
    • ejb - contains the sample SFSB code
    • client - contains application client code

Configuring the Environment

Environment Configuration

Make sure the following environment variables are defined.

  • %ORACLE_HOME% - The directory where you installed OC4J.
  • %JAVA_HOME% - The directory where your JDK is installed
  • %PATH% - includes %ORACLE_HOME% /ant/bin

Starting up Oracle Application Server 10g 10.1.3

Start OC4J stand alone using the following command after you make the above changes.

>%ORACLE_HOME%/bin/oc4j -start

If you are using an OracleAS managed install, start using the following command after you make the above changes.

> %ORACLE_HOME%/opmn/bin/opmnctl startall

4. Generate, Compile, and Deploy the Application

Ant 1.6.2 is shipped with OC4J and you have to set your PATH environment variable to $ORACLE_HOME/ant/bin. 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 ant-oracle.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 for OC4J standalone:

  • 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)

If you are using OracleAS managed install then you have appropriately change the following properties beside changing oc4j.admin.user and oc4j.admin.password for your managed OC4J instance in OracleAS install.

  • opmn.host: the hostname/IP where OracleAS is running (default localhost)
  • opmn.port: OPMN request port (default 6003) for the OracleAS install
  • oc4j.instance: admin user name (default oc4jadmin)

You have to uncomment appropriate deployer.uri in the ant-oracle.properties based on your environment i.e. a single instance OC4J or a clustered OC4J instance/group managed by OPMN.

You have to make changes in jndi.properties such as provider.url, principal and credential appropriate to your environment. If you are using OracleAS install, you have to use provider.url in the following format: opmn:ormi://localhost:6003:home/ejb30compatibility.

Build the Application

In the top-level %HOWTO_HOME% directory, type the command:

>ant

You should now have the newly created ejb30compatibility.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.

Deploy the Application

You can also deploy the application separately by using the following command. Make sure the %ORACLE_HOME% environment variable is defined. In the top-level %HOWTO_HOME% directory, type the command:

>ant deploy

Run the Application

You can run the sample as follows:

>ant run

Now go back to the console where you started OC4J and you will see output generated by the Cart EJB.

[java] Looking up Cart
[java] Adding items to cart
[java] Listing cart contents
[java]   Item1
[java]   Item2

Summary

In this document, you should have:

  • Learned to develop a Stateful Session Bean using EJB 3.0 with an optional EJBHome interface
  • Deployed and executed a simple stateful session bean in the Oracle Application Server 10g 10.1.3.1 without breaking compatibility with EJB 2.x clients
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