Search code examples
javaejbweblogiclookupinitial-context

Lookup EJB using InitialContext on Weblogic 10.x.x


Could you please tell me how to lookup EJB on Weblogic?
I have following bean:

@Stateless
@EJB(name = "DataAccess", beanInterface = DataAccessLocal.class)
public class DataAccess implements DataAccessLocal {
    ...
}

I need this bean in other class which is not part of managed content (just simple class), so I guess it should be done like this:

DataAccessLocal dataAccess = DataAccessLocal.class.cast((new InitialContext()).lookup("%SOME_JNDI_NAME%"));

The question is what should be used as %SOME_JNDI_NAME% in case of Weblogic 10.x.x AS?
Any help will be appreciated.


Solution

  • I would update your EJB class to look like this:

    @Stateless(name="DataAccessBean", mappedName="ejb/DataAccessBean")
    @Remote(DataAccessRemote.class)
    @Local(DataAccessLocal.class)
    public class DataAccess implements DataAccessLocal, DataAccessRemote {
        ...
    }
    

    Looking up the EJB from a class deployed in the same EAR (using the local interface):

    InitialContext ctx = new InitialContext(); //if not in WebLogic container then you need to add URL and credentials.
    // use <MAPPED_NAME>
    Objet obj = ctx.lookup("java:comp/env/ejb/DataAccessBean");
    

    EJB injection is usually preferred, and you can do it as follows:

    @EJB(name="DataAccessBean")
    DataAccessLocal myDataAccessBean;
    

    If you are trying to use the EJB remotely then you will need to use the remote interface and the following JNDI name:

    DataAccessBean#<package>.DataAccessRemote