EJB 3.0 JNDI查找(Weblogic 10.x) [英] EJB 3.0 JNDI lookup (Weblogic 10.x)
问题描述
我使用weblogic 10.3.6以及EJB 3.0。我有EJB和本地接口。两者都装在耳内的ejb-jar中。
@Local
公共接口TestLocal {
...
}
@Stateless
公共类TestEJB实现TestLocal {
...
}
要从战争中访问此EJB,我在 web.xml
< EJB本地-REF>
< ejb-ref-name> ejb / TestLocal< / ejb-ref-name>
< ejb-ref-type> Session< / ejb-ref-type>
< local> testpackage.TestLocal< / local>
< / ejb-local-ref>
查找类似于
test =(TestLocal)new InitialContext()。lookup(java:comp / env / ejb / TestLocal);
一切正常。现在我需要从打包的同一个ejb-jar中调用这个EJB。但是我一直有 javax.naming.NameNotFoundException
。我已经尝试过:
-
在
ejb-jar.xml
在ejb-jar(不是耳朵)< 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>
以及以下查询
initialContext.lookup(java:comp / env / TestEJB);
initialContext.lookup(TestEJB);
-
in
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>
-
两者
weblogic-ejb-jar.xml
和ejb-jar.xml
有你对我做错了什么想法?
Java EE中的JNDI有不同的命名空间。在Java EE 6之前,通常只有全局命名空间和组件环境命名空间。
每个EJB都有自己的组件环境。 Web应用程序也有自己的组件环境。当您使用以 java:comp / env
为前缀的JNDI名称时,这就是您正在查找的内容。通过部署描述符(ejb-jar.xml和web.xml)将对象添加到组件环境中:
< ejb-名称>&TestEJB LT; / EJB-名称>
< 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>
...
因此,来自调用上下文对于TestEJB,您可以查找上面的参考:
TestLocal testLocal =(TestLocal)new InitialContext()。lookup(java: comp / env的/ TestEJB);
JNDI查找名称始终为 java:comp / env /< ejb -ref-名称>
。
您可以看到这实际上正在查找自己的另一个实例,因为这是您在上面声明的内容。 这可能不是你想要的。
鉴于上述情况,你的问题的答案取决于你的POJO的调用情境。 / p>
如果是webapp - > POJO - > TestEJB,那么你应该使用webapp的组件环境并查找java:comp / env / ejb / TestLocal。
如果是webapp - > SomeEJB - > TestEJB,那么你需要向SomeEJB添加一个组件环境(不是你的示例中的TestEJB)并查找你定义的名称那里。
I use weblogic 10.3.6 and so EJB 3.0. I have EJB and local interface. Both packaged in ejb-jar inside ear.
@Local
public interface TestLocal {
...
}
@Stateless
public class TestEJB implements TestLocal {
...
}
To access this EJB from war I have in my 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. But I have javax.naming.NameNotFoundException
all the time. What I have already tried:
In
ejb-jar.xml
in ejb-jar (not ear)<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");
in
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>
Both
weblogic-ejb-jar.xml
andejb-jar.xml
Have you any ideas of what I'm doing wrong?
JNDI in Java EE has different namespaces. Prior to Java EE 6, there is typically only the "global" namespace and the "component environment" namespace.
Each EJB has it's own component environment. A webapp also has it's own component environment. This is what you're looking up when you use a JNDI name prefixed with java:comp/env
. Objects are added to the component environment via deployment descriptors (ejb-jar.xml and 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:
TestLocal testLocal = (TestLocal) new InitialContext().lookup("java:comp/env/TestEJB");
The JNDI lookup name is always 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.
If it is webapp -> POJO -> TestEJB, then you should use the webapp's component environment and lookup "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.
这篇关于EJB 3.0 JNDI查找(Weblogic 10.x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!