Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNDI lookup of EJB3 inside an EAR file on Glassfish

I have an EAR file with a bunch of JARs in it, and one of these JARs contains Local Session Beans (EJB3). I need to perform a JNDI lookup of these Session Beans from within an unmanaged POJO, also contained in the EAR (and in this case in the same JAR as the EJBs as well). I tried following the Glassfish EJB FAQ, but I keep on receiving a javax.naming.NameNotFoundException no matter what I try.

I am unsure of a few things. Where should I put my ejb-jar.xml (I tried the EARs META-INF as well as the JARs META-INF)? Do I need a sun-ejb-jar.xml? What exactly is ejb-link, what does it do? What could I be doing wrong (my configuration is almost identical to the one given in the FAQ for local lookups)?

I list some of the configuration I tried and the result below:

<enterprise-beans>
  <session>
    <ejb-name>ITestBean</ejb-name>
    <ejb-class>com.test.TestBean</ejb-class>
    <ejb-local-ref>
      <ejb-ref-name>ITestBean</ejb-ref-name>
      <local>com.test.ITestBean</local>
    </ejb-local-ref>
  </session>
 </enterprise-beans>

Application deploys but JNDI lookup returns null.

<enterprise-beans>
  <session>
    <ejb-name>ITestBean</ejb-name>
    <ejb-class>com.test.TestBean</ejb-class>
    <ejb-local-ref>
      <ejb-ref-name>ITestBean</ejb-ref-name>
      <local>com.test.ITestBean</local>
      <ejb-link>ITestBean</ejb-link>
    </ejb-local-ref>
  </session>
 </enterprise-beans>

Application doesn't deploy: Unable to determine local business vs. remote business designation for EJB 3.0 ref Unresolved Ejb-Ref ITestBean@jndi.

<enterprise-beans>
  <session>
    <ejb-name>ITestBean</ejb-name>
    <ejb-class>com.test.TestBean</ejb-class>
    <ejb-local-ref>
      <ejb-ref-name>ITestBean</ejb-ref-name>
      <local>com.test.ITestBean</local>
      <ejb-link>MyJar.jar#ITestBean</ejb-link>
    </ejb-local-ref>
  </session>
 </enterprise-beans>

Application doesn't deploy: Error: Unresolved : MyJar.jar#ITestBean.

<enterprise-beans>
  <session>
    <ejb-name>ITestBean</ejb-name>
    <local>com.test.ITestBean</local>
    <ejb-local-ref>
      <ejb-ref-name>ITestBean</ejb-ref-name>
    </ejb-local-ref>
  </session>
 </enterprise-beans>

Error processing EjbDescriptor

like image 998
Zecrates Avatar asked Oct 06 '09 06:10

Zecrates


2 Answers

You can always also dump on System.out or in a log all the names in the InitialContext.

//Get all the names in the initial context
NamingEnumeration children = initialContext.list("");

while(children.hasMore()) {
    NameClassPair ncPair = (NameClassPair)children.next();
    System.out.print(ncPair.getName() + " (type ");
    System.out.println(ncPair.getClassName() + ")");
  }
}
like image 91
elhoim Avatar answered Oct 21 '22 17:10

elhoim


ejb-jar.xml for your ejb file goes into META-INF (of the EJB-Jar, not of the ear). EJB Refs in the deployment descriptor look something like this:

<ejb-local-ref>
    <ejb-ref-name>EJBName</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>classname</local>
    <ejb-link>JARName.jar#EJBName</ejb-link>
</ejb-local-ref>

The lookup code looks something like:

Context c = new InitialContext();
return (EJBLocalInterface) c.lookup("java:comp/env/EJBName");

I don't believe that you will need a container specific deployment descriptor (sun-ejb-jar.xml) for this type of lookup.

like image 2
jsight Avatar answered Oct 21 '22 17:10

jsight