Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Accessing an EJB 2.1 Enterprise Bean

This section describes the following:

Accessing an EJB 2.1 Enterprise Bean Remotely

A remote multitier situation exists when you have the servlets executing in one server, which are to connect and communicate with enterprise beans in another server. Both the servlets and enterprise beans are contained in the same application. When you deploy the application to two different servers, the servlets normally look for the local enterprise bean first.

In Figure 29-1, the HelloBean application is deployed to both server 1 and 2. In order to ensure that the servlets only call out from server 1 to the enterprise beans in server 2, you must set the remote attribute appropriately in the application before deploying on both servers.

Figure 29-1 Multitier Example

Description of Figure 29-1 follows
Description of "Figure 29-1 Multitier Example "

The remote attribute in the <ejb-module> element in orion-application.xml for the EJB module denotes whether the enterprise beans for this application are deployed or not.

  1. In server 1, you must set remote=true in the <ejb-module> element of the orion-application.xml file ,and then deploy the application. The EJB module within the application will not be deployed. Thus, the servlets will not look for the enterprise beans locally, but will go out to the remote server for the EJB requests.

  2. In server 2, you must set remote=false in the <ejb-module> element of the orion-application.xml file and then deploy the application. The application, including the EJB module, is deployed as normal. The default for the remote attribute is false; thus, simply ensure that the remote attribute is not true and redeploy the application.

  3. Configure RMI options:

    • In a standalone OC4J, specify RMI server data in the RMI configuration file, rmi.xml. Specify the location of this file in server.xml, the OC4J configuration file. By default, both these files are installed in <ORACLE_HOME>/j2ee/home/config.

      For more information, see "Configuring RMI in a Standalone OC4J Installation" in the Oracle Containers for J2EE Services Guide.

    • In an Oracle Application Server environment, you must edit the opmn.xml file to specify the port range, on which this local RMI server listens for RMI requests. Note that manual changes to configuration files in an Oracle Application Server environment must be manually updated on each OC4J instance.

      For more information, see "Configuring RMI in an Oracle Application Server Environment" in the Oracle Containers for J2EE Services Guide.

  4. Set JNDI properties java.naming.provider.url and java.naming.factory.initial.

    For more information see the following:

  5. Look up the remote enterprise bean.

    If multiple remote servers are configured, OC4J searches all remote servers for the intended EJB application.

For more information, see "Using Remote Method Invocation in OC4J" in the Oracle Containers for J2EE Services Guide.

Accessing an EJB 2.1 Enterprise Bean Locally

A local multitier situation exists when both the servlets and enterprise beans are contained in the same application and deployed to the same server.

The remote attribute in the <ejb-module> element in orion-application.xml for the EJB module denotes whether the enterprise beans for this application are deployed or not.

  1. In the server, to which you deploy your application, you must set remote=false in the <ejb-module> element of the orion-application.xml file, and then deploy the application. The application, including the EJB module, is deployed as normal. The default for the remote attribute is false.

  2. Set JNDI properties java.naming.provider.url and java.naming.factory.initial.

    For more information see the following:

  3. Look up the local EJB.

Accessing an EJB 2.1 Enterprise Bean Using RMI From a Standalone Java Client

Example 29-33 shows the type of lookup that you can use from a standalone Java client (see "Standalone Java Client") in this release to look up an OC4J-deployed enterprise bean without having to specify an RMI port. Example 29-33 shows how to look up the enterprise bean named MyCart in the Java EE application ejbsamples deployed to the OC4J instance named oc4j_inst1 running on host myServer.

Example 29-33 Accessing an EJB 2.1 Enterprise Bean Using RMI from a Standalone Java Client

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
env.put(Context.SECURITY_CREDENTIALS, "password");
env.put(Context.PROVIDER_URL,"opmn:ormi://myServer:oc4j_inst1/ejbsamples");

Context context = new InitialContext(env);

Object homeObject = context.lookup("MyCart");
CartHome home = (CartHome)PortableRemoteObject.narrow(homeObject,CartHome.class);

For more information, see the following:

Accessing an EJB 2.1 Enterprise Bean From an EJB 3.0 Client

To access an EJB 2.1 enterprise bean from an EJB 3.0 client:

  1. Create an environement reference to the EJB 2.1 enterprise bean's home and remote interface as Example 29-34 shows.

    In this example, you configure an environment reference to the home and remote interface of the EJB 2.1 Scheduler bean. For more information on Job Scheduler, see the Oracle Containers for J2EE Job Scheduler Developer's Guide.

    Example 29-34 Creating an Environment Reference to an EJB 2.1 Enterprise Bean's Home and Remote Interface

    <ejb-ref>
        <ejb-ref-name>ejb/scheduler</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <home>oracle.ias.scheduler.SchedulerHome</home>
        <remote>oracle.ias.scheduler.SchedulerRemote</remote>
    </ejb-ref>
    

    For more information, see Chapter 19, "Configuring JNDI Services"

  2. Access the EJB 2.1 enterprise bean from the EJB 3.0 client:

    An EJB 3.0 client can access an EJB 2.1 enterprise bean in a variety of ways, including, but not limited to, the following:

    1. Inject the EJB 2.1home interface using the @EJB annotation as Example 29-35 shows.

      In this example, you set the @EJB annotation name attribute to the <ejb-ref-name> of the EJB 2.1 enterprise bean.

      Example 29-35 Injecting an EJB 2.1 Home Interface Using @EJB

      ...
      public class MyEJB30Client {
      
          @EJB(name="ejb/scheduler")
          SchedulerHome home;
      
          public void bar() {
              home.create();
              ...
          }
      }
      
    2. Inject the EJB 2.1 home interface using the <injection-target> element in deployment XML.

      Example 29-36 shows how to add an <injection-target> element to the deployment XML to associate the EJB 2.1 home interface with an instance variable named home. As Example 29-37 shows, at deployment time, OC4J will ensure that instance variable home in the EJB 3.0 client is initialized appropriately.

      Example 29-36 Adding an <injection-target> to the Deployment XML

      <ejb-ref>
          <ejb-ref-name>ejb/scheduler</ejb-ref-name>
          <ejb-ref-type>Session</ejb-ref-type>
          <home>oracle.ias.scheduler.SchedulerHome</home>
          <remote>oracle.ias.scheduler.SchedulerRemote</remote>
          <injection-target>
              <injection-target-name>home</injection-target-name>
          </injection-target>
      </ejb-ref>
      

      Example 29-37 Injecting an EJB 2.1 Home Interface Into an Instance Variable Using

      ...
      public class MyEJB30Client {
      
          SchedulerHome home;
      
          public void bar() {
              home.create();
              ...
          }
      }
      
    3. Look up the EJB 2.1 home interface using JNDI as Example 29-38 shows.

      In this example, you look up the EJB 2.1 enterprise bean's <ejb-ref-name> prefixed with java:comp/env/.

      Example 29-38 Performing a JNDI Lookup of the Home Interface

      ...
      public class MyEJB30Client {
      
          SchedulerHome home;
      
          public void bar() {
              InitalContext ic = new InitialContext();
              home = ic.lookup("java:comp/env/ejb/scheduler");
              home.create();
              ...
          }
      }