Java 在 Weblogic 10.3 中调用 EJB 时出现 NameNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1618488/
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
NameNotFoundException when calling a EJB in Weblogic 10.3
提问by Sajee
I have a EJB defined as this:
我有一个 EJB 定义如下:
package com.foo;
@Stateless (mappedName="HelloWorld")
public class HelloWorldBean implements HelloWorld, HelloWorldLocal
....
When it's deployed to Weblogic (WL), it gets the name myBean. I'm not sure if this is important.
当它部署到 Weblogic (WL) 时,它的名称是 myBean。我不确定这是否重要。
I try to call the bean with this code:
我尝试使用以下代码调用 bean:
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
ic = new InitialContext(ht);
tp = (HelloWorld) ic.lookup("HelloWorld#com.foo.HelloWorldBean");
Anyone know why I get the following error?
有谁知道为什么我收到以下错误?
javax.naming.NameNotFoundException: While trying to lookup 'HelloWorld#com.foo.HelloWorldBean' didn't find subcontext 'HelloWorld#com'.
Resolved '' [Root exception is javax.naming.NameNotFoundException: While trying
to lookup 'HelloWorld#com.foo.HelloWorldBean' didn't find
subcontext 'HelloWorld#com'. Resolved '']; remaining name 'HelloWorld#com/foo/HelloWorldBean'
采纳答案by Pascal Thivent
To lookup a Remote Interface of a Session Bean with multiple Remote Business interfaces (e.g.com.acme.FooBusiness1
, com.acme.FooBusiness2
), you need to lookup a name derived from the combination of the target ejb's global JNDI name (the mappedName()
in @Stateless
) and the specific Remote Business Interface, separated by a "#":
要查找具有多个远程业务接口(例如com.acme.FooBusiness1
,com.acme.FooBusiness2
)的会话 Bean 的远程接口,您需要查找从目标 ejb 的全局 JNDI 名称(mappedName()
in @Stateless
)和特定远程业务接口的组合派生的名称,由一个“#”:
InitialContext ic = new InitialContext();
FooBusiness1 bean1 = (FooBusiness1) ic.lookup("FooEJB#com.acme.FooBusiness1");
FooBusiness2 bean2 = (FooBusiness2) ic.lookup("FooEJB#com.acme.FooBusiness2");
In the typical case of a bean only having one Remote Business Interface, this fully-qualified form is not needed. In that case, the bean's JNDI name can be used directly :
在 bean 只有一个远程业务接口的典型情况下,不需要这种完全限定的形式。在这种情况下,可以直接使用 bean 的 JNDI 名称:
FooBusiness bean = (FooBusiness) ic.lookup("FooEJB");
That was the theoretical part. Now the practice. In your case, from what I can see, you are accessing the EJB from Weblogic so I'd rather use the no-arg InitialContext()
constructor (and use a jndi.properties
configuration file for other environments) but this is just a side note. Then, you should look up com.foo.HelloWorld
, the Remote Interface, not com.foo.HelloWorldBean
, the implementation:
那是理论部分。现在实践。在您的情况下,据我所知,您正在从 Weblogic 访问 EJB,因此我宁愿使用无参数InitialContext()
构造函数(并jndi.properties
为其他环境使用配置文件),但这只是一个旁注。然后,您应该查找com.foo.HelloWorld
远程接口,而不是com.foo.HelloWorldBean
实现:
InitialContext ic = new InitialContext();
(HelloWorld) ic.lookup("HelloWorld#com.foo.HelloWorld");
And if your bean has only one Remote Business Interface, this should work:
如果您的 bean 只有一个远程业务接口,这应该可以工作:
(HelloWorld) ic.lookup("HelloWorld");