Lookup EJB using InitialContext on Weblogic 10.x.x
Asked Answered
F

1

7

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.

Forefather answered 17/8, 2011 at 9:22 Comment(3)
I've answered this in another place: https://mcmap.net/q/1621612/-giving-an-ejb-a-jndi/…Missis
Thanks @Nuno. Does it work for remote interface only? Or local too?Forefather
Not sure on that one, since i've been working with remote interfaces onlyMissis
E
9

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
Eslinger answered 17/8, 2011 at 9:22 Comment(5)
Thank you @Jeff, I've just tried it out (with local interface) and got following exception: (I don't have full stacktrace but the main part is) "remaining name: env/ejb/DataAccessBean". Do you have any ideas?Forefather
happy to help via email - [email protected]. We can post the solution here when we are done...Eslinger
Also, if you are trying to access the EJB from outside the EAR where the EJB is deployed, or if the EJB is deployed in its own ejb-jar you will need to sue the remote method.Eslinger
thank you. So far usage of remote interface is enought for me,but that's strange cause I make a call from the same EAR. If you will have any ideas about the problem, it could be nice if you let me know.Forefather
The generated JNDI name only works with the remote interface class. The local interface is not added to JNDI in WebLogic 10.3.x. I describe this at more length hereGeof

© 2022 - 2024 — McMap. All rights reserved.