简体   繁体   中英

Can I lookup an EJB from another EJB?

I have an EJB and it has a method that makes a lookup to another EJB. I'm having a NameNotFoundException with this code.

ServiceEJBImpl :

@Stateless(name = "ServiceEJB", mappedName = "AppAcaAvaliacaoAtuarial-ModelAvaliacaoAtuarial-ServiceEJB")
public class ServiceEJBImpl implements ServiceEJB, ServiceEJBLocal{   

public void persistConflicts(List<Conflict> conflicts)  
{
    ConflictDAOLocal conflictDAO =  (ConflictDAOLocal) BeanLocator.lookup("java:comp/env/ejb/local/ConflictDAO");
    conflictDAO.persistAll(conflicts);
}

ConflictDAOLocal:

@Local
public interface ConflitoDAOLocal{
   void persistAll(List<Conflict> conflicts);
}

That's my lookupMethod:

public static Object lookup(String jndiName) {
    Context context = null;
    try {
        context = new InitialContext();
        return context.lookup(jndiName);
    } catch (NamingException ex) {
        throw new IllegalStateException("...",ex);
    } finally {
        try {
            context.close();
        } catch (NamingException ex) {
            throw new IllegalStateException("...",ex);
        }
    }
}

web.xml:

<ejb-local-ref>
    <ejb-ref-name>ejb/local/ConflictDAO</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
<local>br.gov.rj.rioprev.sigap.acompatuarial.avalatuarial.model.dao.ConflictDAOLocal</local>
    <ejb-link>ConflictDAO</ejb-link>
</ejb-local-ref>

 <ejb-local-ref>
    <ejb-ref-name>ejb/local/ServiceEJB</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>br.gov.rj.rioprev.sigap.acompatuarial.avalatuarial.model.dao.ServiceEJBLocal</local>
    <ejb-link>ServiceEJB</ejb-link>
</ejb-local-ref>

Is there a problem instantiating a new context when making a lookup to an EJB inside another EJB that already has a context instantiated?

I'm not considering @EJB annotation.

As far as you lookup with the correct jndi name you should be able to do it, im not an expert but if you are running this in a web server it would tell you the jndi at momment of the deploy, also you dont really need to create your own lookup, there is one already done that you can use it (actually you are using it but thats just a matter of style), the best aproach maybe is to give your initial context the web server's context, it depends I belive in every web server it should be done something like this

try{             

        Properties prop = new Properties();
        prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); //This is the one that you have to change depending on your webserver
        Context context = new InitialContext(prop);         
         ejbean = (BitacoraBeanRemote)context.lookup("ejb:/EjbSite//BitacoraBean!some.package.bean.BitacoraBeanRemote"); // This is the lookup, it could be a local or a remote interface depending on needs            
    }catch(Exception e){
        e.printStackTrace();
    }

I was writing when you edit your post just make sure your jndi name is fine "java:comp/env/ejb/local/ConflictDAO", you should probably change it you app is not finding this particular jndi

In order to perform a JNDI lookup in an EJB for a dependent EJB, you need to specify your ejb-local-ref elements in an ejb-jar.xml file in you EJB jar:

<ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
          http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
         version="3.2">

    <enterprise-beans>
        ...
        <session>
            <ejb-name>ServiceEJB</ejb-name>

            <ejb-local-ref>
                <ejb-ref-name>ejb/local/ConflictDAO</ejb-ref-name>
                <local>br.gov.rj.rioprev.sigap.acompatuarial.avalatuarial.model.dao.ConflictDAOLocal</local>
                <ejb-link>ConflitoDAO</ejb-link>
            </ejb-local-ref>
        </session>
        ...
    </enterprise-beans>
</ejb-jar>

You must provide an ejb-local-ref element for each EJB that looks up another EJB.

Each EJB in an EJB jar has it's own java:comp/env namespace, whereas a webapp WAR file has a single java:comp/env namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM