Glassfish 上 EAR 文件内 EJB3 的 JNDI 查找

发布于 2024-08-07 00:33:37 字数 2301 浏览 5 评论 0原文

我有一个 EAR 文件,其中包含一堆 JAR,其中一个 JAR 包含本地会话 Bean (EJB3)。我需要从非托管 POJO 中执行这些会话 Bean 的 JNDI 查找,该 POJO 也包含在 EAR 中(在本例中也包含在与 EJB 相同的 JAR 中)。我尝试遵循 Glassfish EJB FAQ,但我继续无论我尝试什么,都会收到 javax.naming.NameNotFoundException 。

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

我列出了我尝试过的一些配置和结果如下:

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

应用程序已部署,但 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>

应用程序未部署:无法确定 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>

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

处理 EjbDescriptor 时出错

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单爱 2024-08-14 00:33:37

您还可以随时在 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() + ")");
  }
}

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() + ")");
  }
}
剩一世无双 2024-08-14 00:33:37

ejb 文件的 ejb-jar.xml 进入 META-INF(EJB-Jar 的,而不是 Ear 的)。部署描述符中的 EJB 引用看起来像这样:

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

查找代码看起来像这样:

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

我不认为您需要容器特定的部署描述符 (sun-ejb-jar.xml) 来进行这种类型的查找。

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.

氛圍 2024-08-14 00:33:37

我认为 EJB 3 可移植性问题 博客文章应该对您有所帮助。

I think the EJB 3 Portability Issue blog post should help you.

回首观望 2024-08-14 00:33:37

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

1) UnmanagementClass1 ->非托管类2 ->非托管类3 ->我的POJO
2) 托管类->非托管类2 ->非托管类3 ->我的POJO

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

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

再可℃爱ぅ一点好了 2024-08-14 00:33:37

我正在研究 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

在 TestBean1 内调用服务定位器

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

将返回 Test2Bean

I have working on glassfish v2 and when calling JSNI from EJB3 I need only ejb-jar.xml, not 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

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

will return Test2Bean

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文