|
Oracle® Application Server Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 2 (10.1.2) Part No. B15505-01 |
|
![]() Previous |
![]() Next |
A 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 2-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.
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.
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.
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.
In the <server> element of the rmi.xml file of server 1, configure the location of server 2, which is the remote server. Provide the hostname, port number, username, and password of the remote server, as follows:
<server host=<remote_host> port=<remote_port> username=<username> password=<password> />
If multiple remote servers are configured, the OC4J container searches all remote servers for the intended EJB application.
Example 2-4 Servlet Accessing EJB in Remote OC4J Instance
The following servlet uses the JNDI name for the target bean: HelloBean. This servlet provides the JNDI properties in an RMIInitialContext object. The environment is initialized as follows:
The INITIAL_CONTEXT_FACTORY is initialized to a RMIInitialContextFactory.
Instead of creating a new InitialContext, it is retrieved.
The actual JNDI name is used in the lookup.
The remote location URL is opmn:ormi://host:oc4j_inst/application. The OPMN port number uses the default and is omitted.
Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, "opmn:ormi://theirhost:oc4j_inst/myapp");
env.put(Context.SECURITY_PRINCIPAL, "admin");
env.put(Context.SECURITY_CREDENTIALS, "welcome");
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.evermind.server.rmi.RMIInitialContextFactory");
Context ic = new com.evermind.server.rmi.RMIInitialContextFactory(). getInitialContext(env);
Object homeObject = ic.lookup("ejb/HelloBean");
// Narrow the reference to a HelloHome.
HelloHome helloHome =
(HelloHome) PortableRemoteObject.narrow(homeObject,
HelloHome.class);