Search code examples
javarestjakarta-eejndistateless-session-bean

JNDI lookup - Is it a correct way in my app?


I am writing an application in Java and I have some REST web services there. My application has following structure: http://cl.ly/L7Pv/o

REST web service classes are Stateless session beans. It works like charm. But Classes in red on the picture want to use that REST resources too.

As fas as I know I cannot use dependency injection and annotation @EJB there. I believe I have to use JNDI lookup. Documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html

But now I dont know how to write this JNDI lookup. I have tried these two:

context.lookup("java:global/diplomka/ListResource");
context.lookup("java:global/Diplomka_maven/ListResource");

What am I doing wrong? Is this a correct approach in the first place?

Thank you


Solution

  • If these classes (ListResource etc.) are Stateless session beans, you can put attribute name or mappedName in @Stateless annotation, e.g.:

    @Stateless(mappedName="ejb/myRestService")
    public class ListResource { ..
    

    Once you have specified JNDI name of your stateless bean, it's easy to fetch the bean through JNDI lookup:

    InitialContext ic = new InitialContext();
    ListResource lr = (ListResource) ic.lookup("ejb/myRestService");
    lr.doWhateverNeeded(..);