Literadix
Literadix

Reputation: 1407

How to get stateless EJBs JNDI path?

Is there a way to get my stateless EJBs JNDI path? Supposing that I have written an EJB like this:

@Stateless
public class BookBean implements IBookBeanLocal {

 @Resource SessionContext sctx;

 ....

 @PostConstruct
 public void afterInit(){
    // sctx??
 }

}

Is there a way do discover the BookBeans JNDI path during initialization? I need this to autosubscribe certain beans to a global registry during initialization. Are there any other ways to do so?

Upvotes: 2

Views: 1798

Answers (2)

rzymek
rzymek

Reputation: 9281

In Java EE 6 the JNDI paths are standarized and documented at https://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html

So assuming you are making JNDI calls only inside one ear:

@Stateless
public class BookBean implements IBookBeanLocal {

    @Resource(lookup = "java:module/ModuleName")
    private String moduleName;

    @PostConstruct
    public void afterInit() {
        String jndi = "java:app/" + moduleName + "/" + getClass().getSimpleName();
    }
}

Or getting the module name via lookup:

@Stateless
public class BookBean implements IBookBeanLocal {
    @PostConstruct
    public void afterInit() {
        Context ctx = new InitialContext();
        String jndi = "java:app/" + ctx.lookup("java:module/ModuleName") + "/" + getClass().getSimpleName();
    }
}

Upvotes: 1

m_vitaly
m_vitaly

Reputation: 11952

You could possibly write the jndi path you want in @Stateless annotation or xml ejb configuration and read that at runtime.

Finding out the auto-generated jndi path is troublesome.

Upvotes: 0

Related Questions