JBoss -> GlassFish: JNDI Lookup of Local Ejb

In JBoss a local ejb can be referenced using a JNDI name such as "jsfejb3/TodoDao/local". That name is not standard. So, use one of the following techniques :

  • reference local ejb using a JNDI name in java:comp/env namespace and add ejb-local-ref element to a deployment descriptor.
  • use dependency injection to inject local ejb reference in the code.

Example: Using java:comp/env namespace

// JBoss
    public class TodoBean {

        private Todo todo;
        ...

        private TodoDaoInt getDao () {
        try {
            InitialContext ctx = new InitialContext();
            return (TodoDaoInt) ctx.lookup("jsfejb3/TodoDao/local");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("couldn't lookup Dao", e);
        }
    }
   
    @Stateless
    public class TodoDao implements TodoDaoInt { ... }
  
    public interface TodoDaoInt { ... }

    *// GlassFish*
    // code changes to TodoBean class
    public class TodoBean {

        private Todo todo;
        ...

        private TodoDaoInt getDao () {
        try {
            InitialContext ctx = new InitialContext();
            return (TodoDaoInt) ctx.lookup("jsfejb3/TodoDao/local");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("couldn't lookup Dao", e);
        }
    }

     InitialContext ctx = new InitialContext();
     return (TodoDaoInt)ctx.lookup("java:comp/env/ejb/TodoDao");
*// add ejb-local-ref for e.g. in web.xml*
    <ejb-local-ref>
        <ejb-ref-name>ejb/TodoDao</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local-home/>
        <local>TodoDaoInt</local>
    </ejb-local-ref>

Dependency injection for injecting ejb references is already covered in other documentation. So no example is shown here.