在Weblogic 10.3中调用EJB时出现NameNotFoundException [英] NameNotFoundException when calling a EJB in Weblogic 10.3
问题描述
我有一个这样定义的EJB:
I have a EJB defined as this:
package com.foo;
@Stateless (mappedName="HelloWorld")
public class HelloWorldBean implements HelloWorld, HelloWorldLocal
....
当部署到Weblogic(WL)时,它将获得名称myBean.我不确定这是否重要.
When it's deployed to Weblogic (WL), it gets the name myBean. I'm not sure if this is important.
我尝试使用以下代码调用Bean:
I try to call the bean with this code:
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'
推荐答案
要查找具有多个远程业务接口(例如,com.acme.FooBusiness1
,com.acme.FooBusiness2
)的会话Bean的远程接口,您需要查找派生自其的名称目标ejb的全局JNDI名称(@Stateless
中的mappedName()
)和特定的远程业务接口的组合,以#"分隔:
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 "#":
InitialContext ic = new InitialContext();
FooBusiness1 bean1 = (FooBusiness1) ic.lookup("FooEJB#com.acme.FooBusiness1");
FooBusiness2 bean2 = (FooBusiness2) ic.lookup("FooEJB#com.acme.FooBusiness2");
在bean仅具有一个远程业务接口的典型情况下,不需要这种完全限定的格式.在这种情况下,可以直接使用Bean的JNDI名称:
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 :
FooBusiness bean = (FooBusiness) ic.lookup("FooEJB");
那是理论部分.现在练习.就您而言,从我所看到的情况来看,您正在从Weblogic访问EJB,所以我宁愿使用no-arg InitialContext()
构造函数(并在其他环境中使用jndi.properties
配置文件),但这只是一个方面笔记.然后,您应该查找com.foo.HelloWorld
(远程接口),而不是com.foo.HelloWorldBean
实施:
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:
InitialContext ic = new InitialContext();
(HelloWorld) ic.lookup("HelloWorld#com.foo.HelloWorld");
如果您的bean只有一个远程业务接口,则应该可以使用:
And if your bean has only one Remote Business Interface, this should work:
(HelloWorld) ic.lookup("HelloWorld");
这篇关于在Weblogic 10.3中调用EJB时出现NameNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!