Problem:
- Product Name: Borland Enterprise Server (AppServer)
- Product Version: 6.7
- Product Component: EJB
- Platform/OS Version: All
- Java Version: 1.5 and 1.6
A multi-tier scenario exists when you have servlets executing in one server, which makes a connection and communicate with EJBs located in another server. Customer requires a proof-of-concept test case to demonstrate calling a EJB in Borland AppServer from a Servlet in WebLogic Server.
Resolution:
Test Scenario:
- Create a servlet in WebLogic Server and make an remote call to the EJB in Borland AppServer.
- We would be using the cart EJB example located in <install_dir>/examples/ejb/basic/cart.
If we take a look in META-INF/ejb-borland.xml in our example cart_beans.jar EJB, we can see that the value for <bean-home-name> is "shoppingcart/remote/cart". This name will be referenced in weblogic.xml in the <jndi-name> tag for remote lookup.
<enterprise-beans>
<session>
<ejb-name>cart</ejb-name>
<bean-home-name>shoppingcart/remote/cart</bean-home-name>
<timeout>0</timeout>
</session>
</enterprise-beans>
First of all, we will create a new web project to generate a WAR file to be deployed into WebLogic Server
In web.xml, we need to add the following configuration section. Note that the <ejb-ref-name> will be the reference name to lookup:
<ejb-ref>
<ejb-ref-name>ejb/cart</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>com.borland.examples.ejb.shoppingcart.CartHome</home>
<remote>com.borland.examples.ejb.shoppingcart.Cart</remote>
</ejb-ref>
In weblogic.xml, we set the <ejb-ref-name> to be "ejb/cart" and the <jndi-name> to reference the ejb in our remote server (i.e. Borland AppServer in our case)
So in the <jndi-name> tag, we specify corbaname followed by the protocol iiop, and then the server host and port. Then we add the # separator, followed by the bean home name of the EJB as specified in ejb-borland.xml, which we will see in the next section.
<ejb-reference-description>
<ejb-ref-name>ejb/cart</ejb-ref-name>
<jndi-name>corbaname:iiop:localhost:2827#shoppingcart/remote/cart</jndi-name>
</ejb-reference-description>
A quick explanation of the jndi-name for corbaname format is as follows:
corbaname:iiop: Object URL
The corbaname:iiop: URL is used to specify the location of a CORBA object in a relatively readable form. It has the following general form:
corbaname:[iiop]:[version@]host[:port][/URL_escaped_object_key]
The protocol identifier iiop is optional. A blank protocol identifier is taken to be iiop by default. The version refers to the IIOP version supported by the object. Currently, it can be 1.0, 1.1, or 1.2. The default is 1.0. The host and port of the server process can be specified. If port is omitted, it will be randomly assigned by OS. URL_escaped_object_key is derived from the object_key that is part of an IOR. The octets of the original object_key are converted to characters using the URL escape mechanism described previously.
Finally, in the doPost() method of our Servlet in WebLogic Server, we can look up the reference to CartHome via the InitialContext with "java:comp/env/ejb/cart":
InitialContext ctx = new InitialContext();
Object ref = ctx.lookup("java:comp/env/ejb/cart");
CartHome home = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);
Once we have the reference to the CartHome, we will be able to call the methods of the cart EJB remotely.
#Security
#VisiBroker