Develop Stateless Session EJB using EJB 3.0

How-To Develop a Stateless Session EJB using EJB 3.0


  • Introduction
    • Stateless Session Bean example using EJB 3.0
    • Sample EJB Client
  • Prerequisites
    • What you need to know
    • Software requirements
    • Notations
  • Building the Application
  • Running the Application
    • Examine the Sample File Directories
    • Configure the Environment
    • Start the Server
    • Generate, Compile and Deploy the Application
    • Run the application
  • Summary

Introduction

This example application demonstrates Oracle's support for the EJB 3.0 specification using a Stateless Session EJB and illustrates the simplified EJB invocation model from an EJB client.

EJB 3.0 greatly simplifies the development of EJBs, removing many complex development tasks. For example, creating a simple stateless EJB using EJB 2.1 requires a bean class and two interfaces, as well as a deployment descriptor. The remote (or local) and home interfaces had to extend javax.ejb.EJBObject and javax.ejb.EJBHome interfaces respectively, and the bean class had to implement the javax.ejb.SessionBean interface. However, in EJB 3.0, development is greatly simplified due to the following specifications:

  • The bean class can be a plain java class (POJO)
  • The EJB interface may be a pure business interface (POJI)
  • The EJB Home interface is no longer needed
  • Annotations can be used instead of a dedicated deployment descriptor

This demonstration uses the HelloWorld bean to demonstrate a simple stateless EJB using EJB 3.0.

Note: Certain EJB 3.0 API may change when EJB 3.0 specification becomes final and you may have to change your application to comply your application with final EJB 3.0 speciation. Oracle cannot guarantee backward compatibility of all EJB 3.0 features in future version of OC4J that complies with final version of the specification.

Stateless Session Bean example using EJB 3.0

The following is the remote interface for the HelloWorld EJB. Note that this is apure Java interfaceand does not extend EJBObject.

package
oracle.ejb30;
import javax.ejb.Remote;
@Remote
public interface HelloWorld
{
public void sayHello(String name);
}

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

package
oracle.ejb30;

import javax.ejb.Stateless;
@Stateless
public class HelloWorldBean implements HelloWorld
{
public void sayHello(String name)
{
System.out.println("Hello "+name +" from
first EJB3.0");

}

Note that the bean uses the @Stateless annotation to mark the bean as a Stateless EJB. @Remote can be used to annotate a remote interface and deployment descriptor for the EJB, but is not required.

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 inject an instance of bean using dependency injection and directly invoke a method on the EJB, as shown in the following client accessor.

@EJB
private static HelloWorld helloWorld;
helloWorld.sayHello("Debu Panda" );

Prerequisites

What you need to know

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

  • EJB 2.1
  • 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 10g 10.1.3.1 Preview
  • %JAVA_HOME% - The directory where your JDK is installed
  • %HOWTO_HOME% - The directory where this demo is unzipped

Building the Application

The Javadoc for this application is located in the %HOWTO_HOME%/doc/javadoc/ directory.

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 10g 10.1.3, 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-slsb.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 that the following environment variables are defined:

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

3. Starting OC4J instance

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/ejb30slsb.

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

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

Return to the console where you started OC4J and you will see output generated by the HelloWorld EJB.

Summary

In this document, you should have learned how to:

  • Develop and deploy a simple stateless session bean using EJB 3.0

0 comments: