Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Accessing an EJB 2.1 EJB

This section describes:

- Accessing an EJB 2.1 EJB Remotely

- Accessing an EJB 2.1 EJB Locally

- Accessing an EJB 2.1 EJB Using RMI from a Stand-Alone Java Client

## Accessing an EJB 2.1 EJB Remotely

A remote multi-tier situation exists when you have the servlets executing in one server which are to connect and communicate with EJBs in another server. Both the servlets and EJBs are contained in the same application. When you deploy the application to two different servers, the servlets normally look for the local EJB 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 EJBs in server 2, you must set the `remote` attribute appropriately in the application before deploying on both servers.

Figure 29-1 Multi-Tier Example

Description of "Figure 29-1 Multi-Tier Example "

The `remote` attribute in the `<ejb-module>` element in `orion-application.xml` for the EJB module denotes whether the EJBs 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 EJBs 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 stand-alone 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:

   - "Configuring the Initial Context Factory"

   - "Setting JNDI Properties for RMI" in the Oracle Containers for J2EE Services Guide.

5. Look up the remote EJB.

   For more information, see:

   - "Looking Up the Remote Interface of an EJB 2.1 EJB Using ejb-ref"

   - "Looking Up the Remote Interface of an EJB 2.1 EJB Using location"

   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 EJB Locally

A local multi-tier situation exists when both the servlets and EJBs 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 EJBs 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:

   - "Configuring the Initial Context Factory"

   - "Setting JNDI Properties for RMI" in the Oracle Containers for J2EE Services Guide.

3. Look up the local EJB.

   For more information, see:

   - "Looking up the Local Interface of an EJB 2.1 EJB Using local-ref"

   - "Looking up the Local Interface of an EJB 2.1 EJB Using local-location"

## Accessing an EJB 2.1 EJB Using RMI from a Stand-Alone Java Client

Example 29-23 shows the type of look up that you can use from a stand-alone Java client (see "Stand-alone Java Client") in this release to look up an OC4J-deployed EJB without having to specify an RMI port. Example 29-23 shows how to look up the EJB named `MyCart` in the J2EE application `ejbsamples` deployed to the OC4J instance named `oc4j_inst1` running on host `myServer`.

Example 29-23 Accessing an EJB 2.1 EJB Using RMI from a Stand-Alone 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:

- "Configuring an Oracle Initial Context Factory"

- "Configuring the Naming Provider URL for OC4J and Oracle Application Server"

- "Configuring the Naming Provider URL for OC4J Standalone"