(nbjrcg) How to find an EJB using Context.lookup (the code generated by NetBeans doesn't work)

View: New views
1 Messages — Rating Filter:   Alert me  

(nbjrcg) How to find an EJB using Context.lookup (the code generated by NetBeans doesn't work)

by Jorge Campins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

I have created an Enterprise Application project using NetBeans. The project contains several EJB modules, J2SE libraries, Web Applications, etc.

In one of the EJB modules I have a Session Bean which I need to access from other EJB modules. To do it from another Session Bean I use the @EJB annotation and I have no problems at all.

But I also would like to access it from a standard java class. The code generated by NetBeans doesn’t work (generated by right click + “Enterprise Resources” + “Call Enterprise Bean”).

This is the code:

private UsuarioFacadeLocal lookupUsuarioFacade() {

    try {

        Context c = new InitialContext();

        return (UsuarioFacadeLocal) c.lookup("java:comp/env/UsuarioFacade");

    } catch (NamingException ne) {

        java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);

        throw new RuntimeException(ne);

    }

}

 

It cannot find the EJB.  At the Glassfish log I can see the corresponding exception stack trace with the message:

javax.naming.NameNotFoundException: java:comp/env/UsuarioFacade not found.

 

I had a very similar problem trying to access a DataSource. The code generated by NetBeans didn’t work (this time generated by right click + “Enterprise Resources” + “Use Database”).

The generated code was:

 

private DataSource getJdbcPublic_PostgreSQL() throws NamingException {

    Context c = new InitialContext();

    return (DataSource) c.lookup("java:comp/env/jdbc/public_PostgreSQL");

}

 

To make it work, I had to check the “Copy Selected Data Source To Project” option at the “Add Data Source Reference” window and then change the generated code. Specifically I had to remove the “java:comp/env/” prefix from the resource name used in the return statement.

So, to call my EJB I tried using "UsuarioFacade" and many other resource names but none of them worked.

Any help will be really appreciated.

Thanks.