Oracle9iAS Containers for J2EE User's Guide Release 2 (9.0.3) Part Number A97681-01 |
|
After you have installed Oracle9iAS Containers for J2EE (OC4J) and configured the base server and default Web site, you can start developing J2EE applications. This chapter assumes that you have a working familiarity with simple J2EE concepts and a basic understanding for EJB development.
This chapter demonstrates simple EJB development with a basic OC4J-specific configuration and deployment. Download the stateless session bean example (stateless.jar
) from the OC4J sample code page on the OTN Web site.
To develop and deploy EJB applications with OC4J, do the following:
For more information on EJBs in OC4J, see Oracle9iAS Containers for J2EE Enterprise JavaBeans Developer's Guide and Reference.
You develop EJB components for the OC4J environment in the same way as in any other standard J2EE environment. Here are the steps to develop EJBs:
Although you can develop your application in any manner, we encourage you to use consistent naming for locating your application easily. One method would be to implement your enterprise Java application under a single parent directory structure, separating each module of the application into its own subdirectory.
Our employee example was developed using the directory structure mentioned in "Creating the Development Directory". Notice in Figure 7-1 that the EJB and Web modules exist under the employee
application parent directory and are developed separately in their own directory.
When you implement an EJB, create the following:
create
method for your bean. If the bean is an entity bean, it also defines the finder method(s) for that bean.
javax.ejb.SessionBean
or javax.ejb.EntityBean
interfaces.
ejbCreate
and ejbPostCreate
methods with parameters matching those of the create
method as defined in the home interfaces.
The home interfaces (remote and local) are used to create the bean instance; thus, they define the create
method for your bean. Each type of EJB can define the create
method in the following ways:
For each create
method, a corresponding ejbCreate
method is defined in the bean implementation.
Any remote client invokes the EJB through its remote interface. The client invokes the create
method that is declared within the remote home interface. The container passes the client call to the ejbCreate
method--with the appropriate parameter signature--within the bean implementation. You can use the parameter arguments to initialize the state of the new EJB object.
javax.ejb.EJBHome
interface.
create
methods must throw the following exceptions:
The following code sample illustrates a remote home interface for a session bean called EmployeeHome
.
package employee; import javax.ejb.*; import java.rmi.*; public interface EmployeeHome extends EJBHome { public Employee create() throws CreateException, RemoteException; }
An EJB can be called locally from a client that exists in the same container. Thus, a collocated bean, JSP, or servlet invokes the create
method that is declared within the local home interface. The container passes the client call to the ejbCreate
method--with the appropriate parameter signature--within the bean implementation. You can use the parameter arguments to initialize the state of the new EJB object.
javax.ejb.EJBLocalHome
interface.
create
methods must throw the following exceptions:
The following code sample shows a local home interface for a session bean called EmployeeLocalHome
.
package employee; import javax.ejb.*; public interface EmployeeLocalHome extends EJBLocalHome { public EmployeeLocal create() throws CreateException, EJBException; }
The component interfaces define the business methods of the bean that a client can invoke.
The remote interface defines the business methods that a remote client can invoke. Here are the requirements for developing the remote interface:
javax.ejb.EJBObject
interface, and its methods must throw the java.rmi.RemoteException
exception.
public
for remote clients.
EJBException
and RemoteException
, are transferred back to the client as remote runtime exceptions.
The following code sample shows a remote interface called Employee with its defined methods, each of which will be implemented in the stateless session bean.
package employee; import javax.ejb.*; import java.rmi.*; import java.util.*; public interface Employee extends EJBObject { public Collection getEmployees() throws RemoteException; public EmpRecord getEmployee(Integer empNo) throws RemoteException; public void setEmployee(Integer empNo, String empName, Float salary) throws RemoteException; public EmpRecord addEmployee(Integer empNo, String empName,
Float salary) throws RemoteException; public void removeEmployee(Integer empNo) throws RemoteException; }
The local interface defines the business methods of the bean that a local (collocated) client can invoke.
javax.ejb.EJBLocalObject
interface.
public
.
The following code sample contains a local interface called EmployeeLocal
with its defined methods, each of which will be implemented in the stateless session bean.
package employee; import javax.ejb.*; import java.rmi.*; import java.util.*; public interface EmployeeLocal extends EJBLocalObject { public Collection getEmployees() throws EJBException; public EmpRecord getEmployee(Integer empNo) throws FinderException, EJBException; public void setEmployee(Integer empNo, String empName, Float salary) throws FinderException, EJBException; public EmpRecord addEmployee(Integer empNo, String empName, Float salary) throws CreateException, EJBException; public void removeEmployee(Integer empNo) throws RemoveException, EJBException; }
The bean contains the business logic for your application. It implements the following methods:
The bean in the example application consists of one class, EmployeeBean
, that retrieves an employee's information.
SessionBean
or EntityBean
interface. The container uses these methods for controlling the life cycle of the bean. These include the ejb<Action>
methods, such as ejbActivate
, ejbPassivate
, and so on.
ejbCreate
methods that correspond to the create
method(s) that are declared in the home interfaces. The container invokes the appropriate ejbCreate
method when the client invokes the corresponding create
method.
The following code shows the bean implementation for the employee example. To compact this example, the try blocks for error processing are removed. See the full example on http://otn.oracle.com
.
package employee; import javax.ejb.*; import java.rmi.*; import java.util.*; import javax.naming.*; public class EmployeeBean extends Object implements SessionBean { public SessionContext ctx; public EmployeeLocal empLocal; public EmployeeBean() {} public EmpRecord addEmployee(Integer empNo, String empName,
Float salary) throws CreateException { return empLocal.addEmployee(empNo, empName, salary); } public Collection getEmployees() { return empLocal.getEmployees(); } public EmpRecord getEmployee(Integer empNo) throws FinderException { return empLocal.getEmployee(empNo); } public void setEmployee(Integer empNo, String empName, Float salary) throws FinderException { empLocal.setEmployee(empNo, empName, salary); } public void removeEmployee(Integer empNo) throws RemoveException { empLocal.removeEmployee(empNo); } public void ejbCreate() throws CreateException { // stateless bean has create method with no args. This // causes one bean instance to which multiple employees cling. } public void ejbRemove() { empLocal = null; } public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext ctx) throws EJBException { this.ctx = ctx; Context context = new InitialContext(); /*Lookup the EmployeeLocalHome object. The reference is retrieved from the application-local context (java:comp/env). The variable is specified in the assembly descriptor (META-INF/ejb-jar.xml). */ Object homeObject = context.lookup("java:comp/env/EmployeeLocalBean"); // Narrow the reference to EmployeeHome. EmployeeLocalHome home = (EmployeeLocalHome) homeObject; // Create remote object and narrow the reference to Employee. empLocal = (EmployeeLocal) home.create(); } public void unsetSessionContext() { this.ctx = null; } }
After implementing and compiling your classes, you must create the standard J2EE EJB deployment descriptor for all beans in the module. The XML deployment descriptor (defined in the ejb-jar.xml
file) describes the EJB module of the application. It describes the types of beans, their names, and attributes. The structure for this file is mandated in the DTD file, which is provided at " http://java.sun.com/dtd/ejb-jar_2_0.dtd".
After creation, place the deployment descriptors for the EJB application in the META-INF
directory that is located in the same directory as the EJB classes. See Figure 7-1 for more information.
The following example shows the sections that are necessary for the Employee
example, which implements both a remote and a local interface.
The following is the deployment descriptor for a version of the employee example that uses a stateless session bean.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <session> <description>Session Bean Employee Example</description> <ejb-name>EmployeeBean</ejb-name> <home>employee.EmployeeHome</home> <remote>employee.Employee</remote><local-home>employee.EmployeeLocalHome</local-home>
<local>employee.EmployeeLocal</local>
<ejb-class>employee.EmployeeBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar>
After you have finalized your implementation and created the deployment descriptors, archive your EJB application into a JAR file. The JAR file should include all EJB application files and the deployment descriptor.
Note: If you have included a Web application as part of this enterprise Java application, follow the instructions for building the Web application in the Oracle9iAS Containers for J2EE User's Guide. |
For example, to archive your compiled EJB class files and XML files for the Employee
example into a JAR file, perform the following in the ../employee/ejb_module
directory:
% jar cvf Employee-ejb.jar .
This archives all files contained within the ejb_module
subdirectory within the JAR file.
Before deploying, perform the following:
application.xml
file with the modules of the enterprise Java application.
The application.xml
file acts as the manifest file for the application and contains a list of the modules that are included within your enterprise application. You use each <module>
element defined in the application.xml
file to designate what comprises your enterprise application. Each module describes one of three things: EJB JAR, Web WAR, or any client files. Respectively, designate the <ejb>
, <web>
, and <java>
elements in separate <module>
elements.
<ejb>
element specifies the EJB JAR filename.
<web>
element specifies the Web WAR filename in the <web-uri>
element, and its context in the <context>
element.
<java>
element specifies the client JAR filename, if any.
As Figure 7-2 shows, the application.xml
file is located under a META-INF
directory under the parent directory for the application. The JAR, WAR, and client JAR files should be contained within this directory. Because of this proximity, the application.xml
file refers to the JAR and WAR files only by name and relative path--not by full directory path. If these files were located in subdirectories under the parent directory, then these subdirectories must be specified in addition to the filename.
For example, the following example modifies the <ejb>
, <web>
, and <java>
module elements within application.xml
for the Employee EJB application that also contains a servlet that interacts with the EJB.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd"> <application> <module> <ejb>Employee-ejb.jar</ejb> </module> <module> <web> <web-uri>Employee-web.war</web-uri> <context-root>/employee</context-root> </web> </module> <module> <java>Employee-client.jar</java> </module> </application>
Create the EAR file that contains the JAR, WAR, and XML files for the application. Note that the application.xml
file serves as the EAR manifest file.
To create the Employee.EAR
file, execute the following in the employee directory contained in Figure 7-2:
% jar cvf Employee.ear .
This step archives the application.xml
, the Employee-ejb.jar
, the Employee-web.war
, and the Employee-client.jar
files into the Employee.ear
file.
After archiving your application into an EAR file, deploy the application to OC4J. See "Deploying Applications" for information on how to deploy your application.
All EJB clients--including standalone clients, servlets, JSPs, and JavaBeans--perform the following steps to instantiate a bean, invoke its methods, and destroy the bean:
create
method on the home interface causes a new bean to be instantiated and returns a bean reference.
remove
method. This will either remove the bean instance or return it to a pool. The container controls how to act on the remove
method.
The following example is executed from a servlet that acts as a remote client. Any remote client must set up JNDI properties before retrieving the object, using a JNDI lookup.
This code should be executed within a TRY block for catching errors, but the TRY block was removed to show the logic clearly. See the example for the full exception coverage.
public class EmployeeServlet extends HttpServlet { EmployeeHome home; Employee empBean; public void init() throws ServletException { /* initialize JNDI context by setting factory, url, and credentials in a hashtable */ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.evermind.server.rmi.ApplicationClientInitialContextFactory"); env.put(Context.PROVIDER_URL, "ormi://myhost/employee"); env.put(Context.SECURITY_PRINCIPAL, "admin"); env.put(Context.SECURITY_CREDENTIALS, "welcome"); /*1. Retrieve remote interface using a JNDI lookup*/ Context context = new InitialContext(); /** * Lookup the EmployeeHome object. The reference is retrieved from the * application-local context (java:comp/env). The variable is * specified in the application-client.xml). */ Object homeObject = context.lookup("java:comp/env/EmployeeBean"); //2. Narrow the reference to EmployeeHome. Since this is a remote
// object, use the PortableRemoteObject.narrow method. EmployeeHome home = (EmployeeHome)
PortableRemoteObject.narrow(homeObject, EmployeeHome.class); //3. Create the remote object and narrow the reference to Employee. Employee empBean = (Employee)
PortableRemoteObject.narrow(home.create(), Employee.class); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); ServletOutputStream out = response.getOutputStream(); //4. Invoke a business method on the remote interface reference. Collection emps = empBean.getEmployees(); out.println("<html>"); out.println( "<head><title>Employee Bean</title></head>"); out.println( "<body>"); out.println( "<table border='2'>"); out.println( "<tr><td>" + "<b>EmployeeNo</b>" + "</td><td>" + "<b>EmployeeName</b>" + "</td><td>" + "<b>Salary</b>" + "</td></tr>"); Iterator iterator = emps.iterator(); while(iterator.hasNext()) { EmpRecord emp = (EmpRecord)iterator.next(); out.println( "<tr><td>" + emp.getEmpNo() + "</td><td>" + emp.getEmpName() + "</td><td>" + emp.getSalary() + "</td></tr>"); } out.println( "</table>"); out.println( "</body>"); out.println("</html>"); out.close(); } }
The following example is executed from a session bean that is collocated with the Employee bean. Thus, the session bean uses the local interface, and the JNDI lookup does not require JNDI properties.
This code should be executed within a TRY block for catching errors, but the TRY block was removed to show the logic clearly. See the example for the full exception coverage.
// 1. Retreive the Home Interface using a JNDI Lookup //Retrieve the initial context for JNDI. No properties needed when local Context context = new InitialContext(); //Retrieve the home interface using a JNDI lookup using // the java:comp/env bean environment variable specified in web.xml Object homeObject = context.lookup("java:comp/env/EmployeeLocalBean"); //2. Narrow the returned object to be an EmployeeHome object. Since // the client is local, cast it to the correct object type. EmployeeLocalHome home = (EmployeeLocalHome) homeObject; //3. Create the local Employee bean instance, return the reference Employee empBean = (Employee) home.create(); //4. Invoke a business method on the local interface reference. Collection emps = empBean.getEmployees(); ...
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|