简体   繁体   English

在Glassfish上的EAR文件中查找EJB3的JNDI

[英]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). 我有一个包含大量JAR的EAR文件,其中一个JAR包含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). 我需要从非托管POJO中执行这些会话Bean的JNDI查找,这些POJO也包含在EAR中(在这种情况下也与EJB中的JAR相同)。 I tried following the Glassfish EJB FAQ , but I keep on receiving a javax.naming.NameNotFoundException no matter what I try. 我尝试了遵循Glassfish EJB FAQ ,但无论我尝试什么,我都会继续收到javax.naming.NameNotFoundException。

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)? 我应该在哪里放置我的ejb-jar.xml(我尝试过EARs META-INF以及JARs META-INF)? Do I need a sun-ejb-jar.xml? 我需要sun-ejb-jar.xml吗? What exactly is ejb-link, what does it do? 究竟什么是ejb-link,它做了什么? 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. 应用程序部署但JNDI查找返回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. 应用程序未部署:无法确定EJB 3.0 ref的未解决的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. 应用程序未部署:错误:未解决: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 处理EjbDescriptor时出错

You can always also dump on System.out or in a log all the names in the InitialContext. 您始终可以在System.out或日志中转储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() + ")");
  }
}

ejb-jar.xml for your ejb file goes into META-INF (of the EJB-Jar, not of the ear). ejb文件的ejb-jar.xml进入META-INF(EJB-Jar,而不是耳朵)。 EJB Refs in the deployment descriptor look something like this: 部署描述符中的EJB Refs看起来像这样:

<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. 我不认为您需要一个特定于容器的部署描述符(sun-ejb-jar.xml)来进行此类查找。

我认为EJB 3 Portability Issue博客文章可以帮助您。

It isn't possible to perform a JNDI lookup of a bean from a POJO, unless that POJO is called (directly or indirectly) from a managed class (such as a session bean). 除非从托管类(例如会话bean)(直接或间接)调用POJO,否则无法从POJO执行bean的JNDI查找。 In other words, the first example won't work while the second one will (assuming MyPOJO is the class that tries to perform a JNDI lookup): 换句话说,第一个示例将不起作用,而第二个示例将(假设MyPOJO是尝试执行JNDI查找的类):

1) UnmanagedClass1 -> UnmanagedClass2 -> UnmanagedClass3 -> MyPOJO 1)UnmanagedClass1 - > UnmanagedClass2 - > UnmanagedClass3 - > MyPOJO
2) ManagedClass -> UnmanagedClass2 -> UnmanagedClass3 -> MyPOJO 2)ManagedClass - > UnmanagedClass2 - > UnmanagedClass3 - > MyPOJO

I have working on glassfish v2 and when calling JSNI from EJB3 I need only ejb-jar.xml, not sun-ejb-jar.xml 我正在研究glassfish v2,当从EJB3调用JSNI时,我只需要ejb-jar.xml,而不是sun-ejb-jar.xml

<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee" 
     version = "3.0" 
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">

         <enterprise-beans>

             <session>
                 <ejb-name>TestBean1</ejb-name>
                 <ejb-local-ref>
                    <ejb-ref-name>ejb/TestLocal</ejb-ref-name>
                    <ejb-ref-type>Session</ejb-ref-type>
                    <local>com.clients.TestLocal</local>
                    <mapped-name>ejb/TestLocal</mapped-name>
                </ejb-local-ref>
                 <ejb-local-ref>
                    <ejb-ref-name>ejb/Test2Local</ejb-ref-name>
                    <ejb-ref-type>Session</ejb-ref-type>
                    <local>com.clients.Test2Local</local>
                    <mapped-name>ejb/Test2Local</mapped-name>
                </ejb-local-ref>
              <session>
        <enterprise-beans>

@Stateless(mappedName="ejb/TestLocal")
public class TestBean1 implements TestLocal

@Stateless(mappedName="ejb/Test2Local")
public class TestBean2 implements Test2Local

Calling the service locator inside TestBean1 as 将TestBean1中的服务定位器调用为

ic.lookup("java:comp/env/ejb/Test2Local");

will return Test2Bean 将返回Test2Bean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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