This demo application showcases the support Oracle Application Server has for
the Stateful Session Bean and callback for lifecycle events using EJB 3.0. It utilizes a Stateful
EJB to demonstrate the new capabilities of EJB 3.0.
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 need a EJB Home 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.
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.PostConstruct;
import javax.ejb.Stateful;
@Stateful
public class CartBean implements Cart {
private ArrayList items;
@PostConstruct
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;
}
}
The javax.ejb.Stateful annotation marks the bean as a stateful session bean. The bean name will be defaulted to CartBean. Because the bean does not implement javax.ejb.SessionBean, we use the javax.ejb.PostConstruct annotation to assign the initialize() method the same role as ejbCreate() in a 2.1 bean.
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.
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 use dependency injection and directly invoke
a method on the EJB.
Here is a snippet from
client code that accesses the EJB
public class CartClient {
@EJB private static Cart cart; public static void main(String[] args) throws Exception {
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
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-sfsb.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.1
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 demodirectory) 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/ejb30sfsb.
Build the Application
In the top-level %HOWTO_HOME% directory, type the command:
>ant
You should now have the newly created ejb30sfsb.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.
Summary
In this document, you should have:
Learned to develop a Stateful Session Bean using EJB 3.0
Deployed and executed a simple stateful session bean in the Oracle Application
Server 10g 10.1.3 .1