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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1523984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:54:57  来源:igfitidea点击:

JNDI lookup of EJB3 inside an EAR file on Glassfish

javaejb-3.0jndilocal

提问by Zecrates

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.

我有一个包含一堆 JAR 的 EAR 文件,其中一个 JAR 包含本地会话 Bean (EJB3)。我需要从非托管 POJO 中对这些会话 Bean 执行 JNDI 查找,该 POJO 也包含在 EAR 中(在本例中,也包含在与 EJB 相同的 JAR 中)。我尝试遵循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)? 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)?

我不确定一些事情。我应该把 ejb-jar.xml 放在哪里(我尝试了 EAR META-INF 和 JAR META-INF)?我需要一个 sun-ejb-jar.xml 吗?ejb-link 到底是什么,它有什么作用?我可能做错了什么(我的配置与本地查找常见问题解答中给出的配置几乎相同)?

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 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.

应用程序未部署:错误:未解决: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

采纳答案by Zecrates

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). 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):

不可能从 POJO 执行 bean 的 JNDI 查找,除非从托管类(例如会话 bean)调用(直接或间接)该 POJO。换句话说,第一个示例将不起作用,而第二个示例将起作用(假设 MyPOJO 是尝试执行 JNDI 查找的类):

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

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

回答by elhoim

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

您也可以随时将 InitialContext 中的所有名称转储到 System.out 或日志中。

//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() + ")");
  }
}

回答by jsight

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 文件的 ejb-jar.xml 进入 META-INF(EJB-Jar,而不是耳朵)。部署描述符中的 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) 来进行此类查找。

回答by elhoim

I think the EJB 3 Portability Issueblog post should help you.

我认为EJB 3 Portability Issue博客文章应该对您有所帮助。

回答by velabhai

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