简体   繁体   English

EJB 3.0 JNDI查找(Weblogic 10.x)

[英]EJB 3.0 JNDI lookup (Weblogic 10.x)

I use weblogic 10.3.6 and so EJB 3.0. 我使用weblogic 10.3.6,所以使用EJB 3.0。 I have EJB and local interface. 我有EJB和本地接口。 Both packaged in ejb-jar inside ear. 两者都包装在耳朵内的ejb-jar中。

@Local
public interface TestLocal {
...
}

@Stateless
public class TestEJB implements TestLocal {
...
}

To access this EJB from war I have in my web.xml 为了从战争中访问这个EJB,我在我的web.xml

<ejb-local-ref>
  <ejb-ref-name>ejb/TestLocal</ejb-ref-name>
  <ejb-ref-type>Session</ejb-ref-type>
  <local>testpackage.TestLocal</local>
</ejb-local-ref>  

And lookup looks like 查找看起来像

test = (TestLocal) new InitialContext().lookup("java:comp/env/ejb/TestLocal");

Everything works fine. 一切正常。 Now I need to call this EJB from the same ejb-jar where it packaged. 现在,我需要从与打包时相同的ejb-jar中调用此EJB。 But I have javax.naming.NameNotFoundException all the time. 但是我一直都有javax.naming.NameNotFoundException What I have already tried: 我已经尝试过的:

  1. In ejb-jar.xml in ejb-jar (not ear) ejb-jar.xml jar中的ejb-jar.xml中(不是耳朵)

     <ejb-name>TestEJB</ejb-name> <ejb-local-ref> <ejb-ref-name>TestEJB</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local>testpackage.TestLocal</local> <ejb-link>myjar.jar#TestEJB</ejb-link> </ejb-local-ref> 

and following lookups 和后续查询

 initialContext.lookup("java:comp/env/TestEJB");
 initialContext.lookup("TestEJB");
  1. in weblogic-ejb-jar.xml weblogic-ejb-jar.xml

     <weblogic-enterprise-bean> <ejb-name>TestEJB</ejb-name> <jndi-name>TestEJB</jndi-name> <local-jndi-name>TestEJB</local-jndi-name> <enable-call-by-reference>True</enable-call-by-reference> </weblogic-enterprise-bean> 
  2. Both weblogic-ejb-jar.xml and ejb-jar.xml weblogic-ejb-jar.xmlejb-jar.xml

Have you any ideas of what I'm doing wrong? 您对我在做什么错有任何想法吗?

JNDI in Java EE has different namespaces. Java EE中的JNDI具有不同的名称空间。 Prior to Java EE 6, there is typically only the "global" namespace and the "component environment" namespace. 在Java EE 6之前,通常只有“全局”名称空间和“组件环境”名称空间。

Each EJB has it's own component environment. 每个EJB都有自己的组件环境。 A webapp also has it's own component environment. Web应用程序还具有自己的组件环境。 This is what you're looking up when you use a JNDI name prefixed with java:comp/env . 这是使用以java:comp/env的JNDI名称时要查找的内容。 Objects are added to the component environment via deployment descriptors (ejb-jar.xml and web.xml): 对象通过部署描述符(ejb-jar.xml和web.xml)添加到组件环境中:

    <ejb-name>TestEJB</ejb-name>
    <ejb-local-ref>
         <ejb-ref-name>TestEJB</ejb-ref-name>
         <ejb-ref-type>Session</ejb-ref-type>
         <local>testpackage.TestLocal</local>
         <ejb-link>myjar.jar#TestEJB</ejb-link>
    </ejb-local-ref>
    ...

So, from within the calling context of TestEJB, you would lookup the reference above: 因此,从TestEJB的调用上下文中,您将查找上面的参考:

  TestLocal testLocal = (TestLocal) new InitialContext().lookup("java:comp/env/TestEJB");

The JNDI lookup name is always java:comp/env/<ejb-ref-name> . JNDI查找名称始终为java:comp/env/<ejb-ref-name>

You can see this actually looking up another instance of itself, because that is what you have declared above. 您可以看到它实际上在查找自身的另一个实例,因为这是您在上面声明的内容。 It's possible this is not what you intended. 这可能不是您想要的。

Given the above, the answer to your question depends upon the calling context of your POJO. 鉴于以上所述,对您问题的答案取决于POJO的调用上下文。

If it is webapp -> POJO -> TestEJB, then you should use the webapp's component environment and lookup "java:comp/env/ejb/TestLocal". 如果是webapp-> POJO-> TestEJB,则应该使用webapp的组件环境并查找“ java:comp / env / ejb / TestLocal”。

If it is webapp -> SomeEJB -> TestEJB, then you need to add a component environment to SomeEJB (not TestEJB as you have in your example) and lookup the name that you define there. 如果是webapp-> SomeEJB-> TestEJB,则需要将组件环境添加到SomeEJB(而不是示例中的TestEJB),并查找在其中定义的名称。

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

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