Accessing an EJB 3.0 from Servlet 2.5 Container

Here is a guide on creating a Stateless EJB for a 3.0 container and accessing it from a Servlet 2.5 container via Websphere 7 and JBoss 7.1. It’s by no means meant to be the only way to configure this scenario. Let’s get on with it.

Accessing a Stateless/Stateful EJB 3.0 from a Servlet 2.5 container

EJB Module

All the configuration here is for the EJB Module, this is typically contained in the ejb.jar.

Create my EJB

With EJB 3.0 you can create an EJB from a simple POJO by adding the below annotation.

@Stateless

My EJB looks like this

package my.project.ejb;

import javax.ejb.Stateless;

@Stateless(name = "MyEjb")
public class MyEjb implements MyLocal
{
	public Status myLocalBusinessMethod()
	{
		System.out.println("Hello from my EJB");
	}
}

Create a local interface

This is your local interface. Local interfaces were introduced, in EJB 2.0, so clients in the same JVM with the EJB container can call EJBs directly. This negates the RMI call overhead that you use to have to suffer when using the Remote Interface to lookup EJBs even if the client was in the same JVM as the EJB container.

	
package my.project.ejb;

import javax.ejb.Local;

@Local
public interface MyLocal
{
	public Status myLocalBusinessMethod();
}

Add an entry to ejb-jar.xml

(only need this if defining some resource references that the EJB uses????)

	
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" metadata-complete="false" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">

    <enterprise-beans>
        <session>
            <ejb-name>MyEjb</ejb-name>
        </session>
    </enterprise-beans>

</ejb-jar>

In the ibm-ejb-jar-bnd.xml add a binding for this EJB

	
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://websphere.ibm.com/xml/ns/javaee" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd" version="1.0">

    <session name="MyEjb" simple-binding-name="ejb/session/MyEjb">
    </session>

</ejb-jar-bnd>

Add a binding to the jboss-ejb3.xml

	
<?xml version="1.0"?>
<jboss:ejb-jar version="3.1" impl-version="2.1" xmlns:jboss="http://www.jboss.com/xml/ns/javaee" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_1.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">

    <enterprise-beans>
        <session>
            <ejb-name>MyEjb</ejb-name>
            <ejb-class>my.project.ejb.MyEjb</ejb-class>
        </session>
    </enterprise-beans>

</jboss:ejb-jar>

WEB Module

Create your client in the web module

	
package my.project;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import my.project.ejb.MyLocal;

public class MyClient {

    public void callMyLocalBusinessMethod()
    {
        try
        {
            InitialContext initialContext = new InitialContext();
            MyLocal myLocal= (MyLocal)initialContext.lookup("java:comp/env/ejb/session/MyEjb");
            myLocal.myLocalBusinessMethod();
        }
        catch (NamingException e)
        {
        }
    }
}

Add a EJB resource reference to the web.xml

(Do we need the ejb-link?)

	
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    
    <ejb-ref>
        <ejb-ref-name>ejb/session/MyEjb</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <ejb-link>MyEjb</ejb-link>
    </ejb-ref>
    
</web-app>

In the ibm-web-bnd.xml add the binding

	
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://websphere.ibm.com/xml/ns/javaee" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd" version="1.0">
    
    <ejb-ref name="ejb/session/MyEjb" binding-name="ejb/session/MyEjb"/>

</web-bnd>

Add a binding to the jboss-web.xml

	
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>

    <ejb-ref>
        <ejb-ref-name>ejb/session/MyEjb</ejb-ref-name>
        <jndi-name>java:app/ejb-jar/MyEjb</jndi-name>
    </ejb-ref>

</jboss-web>