EJB Reference

An EJB client can define EJB references, logical names, or nicknames, used by the client to find the EJB homes. When using references, the client can hard code the name of the reference. Then, during deployment, the reference is mapped to the real name in the JNDI namespace to which the EJB is bound. A reference to an EJB specifies either the local or remote home of the EJB.

For remote home interfaces, using EJB references is an option, but also a best
practice. For local home interfaces, however, it is a must, because using an EJB
reference is the only way an EJB client can look up a local home interface.

In my sample HelloWorld EAR application, i do have an HelloWorldEJB which is accessed by HelloWorldServlet. I wanted to try the reference binding so i followed these steps.

First i opened the deployment descriptor of HelloWorldWeb using the Web Application Deployment descriptor editor. In that i went to References tab click on add, and select EJB Reference check box click next. On the next page it displays the HelloWorldEJB project and HelloWorld EJB that is part of the project so i selected it




Now when i looked at the web.xml file i could see a EJB reference element added into it like this

<ejb-ref id="EjbRef_1252108343393">
<description id="Description_1252113864562">
</description>
<ejb-ref-name>ejb/HelloWorld</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>ejbs.HelloWorldHome</home>
<remote>ejbs.HelloWorld</remote>
<ejb-link>HelloWorldEJB.jar#HelloWorld</ejb-link>
</ejb-ref>


In addition to this reference the ibm-web-bnd.xmi file was created in the WEB-INF folder of HelloWorldWeb project it looked like this


<?xml version="1.0" encoding="UTF-8"?>
<webappbnd:WebAppBinding xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:webappbnd="webappbnd.xmi" xmi:id="WebAppBinding_1251663066218" virtualHostName="default_host">
<webapp href="WEB-INF/web.xml#WebApp_ID"/>
<ejbRefBindings xmi:id="EjbRefBinding_1252108343393" jndiName="ejb/ejbs/HelloWorldHome">
<bindingEjbRef href="WEB-INF/web.xml#EjbRef_1252108343393"/>
</ejbRefBindings>
</webappbnd:WebAppBinding>


As you can see the ibm-web-bnd.xmi has a ejbRefBindings element with value of id attribute equal to the value of id attribute of ejb-ref element in web.xml so that means this binding is for the ejb refernece defined in web.xml. If you look at it the value of jndiName is equal to "ejb/ejbs/HelloWorldHome", which is the JNDI name for HelloWorldHome

Now inside my HelloWorldServlet i can call the HelloWorldEJB methods like this

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Inside HelloWorldServlet.doGet()");
try {
Context ctx = new InitialContext();
Object obj = ctx.lookup("java:comp/env/ejb/HelloWorld");
HelloWorldHome helloWorldHome = (HelloWorldHome)obj;
HelloWorld helloWorld = helloWorldHome.create();
String aString = helloWorld.sayHello();
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
}
getServletContext().getRequestDispatcher("/index.jsp").include(request , response);
}


The first part of the context lookup is always java:comp/env followed by name of the ejb reference as defined in the web.xml.

If the EJB that your web application is trying to access is not part of the same EAR then you will have to copy the client jar file in sharedlib and then map the actual JNDI name of the bean to the resource reference at the time of deployment

If you want you can download sample code from here

1 comment:

Unknown said...

where you set de host and port of the server that expose the EJB?