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.
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 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/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
Deploy and execute a simple stateless session bean in the Oracle
Application Server 10g 10.1.3.1