我将EJB定义为:

package com.foo;
@Stateless (mappedName="HelloWorld")
public class HelloWorldBean implements HelloWorld, HelloWorldLocal
....

当它部署到Weblogic(WL)时,它获得名称myBean。我不确定这是否重要。

我尝试使用以下代码调用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");

任何人都知道我为什么会收到以下错误?

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'
有帮助吗?

解决方案

要查找具有多个远程业务接口的会话Bean的远程接口(例如 com.acme.FooBusiness1 com.acme.FooBusiness2 ),您需要查找从目标ejb的全局JNDI名称( @Stateless 中的 mappedName())和特定远程业务接口的组合派生的名称,以“#”分隔;:

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名称可以直接使用:

FooBusiness bean = (FooBusiness) ic.lookup("FooEJB");

这是理论部分。现在的做法。在你的情况下,从我所看到的,你是从Weblogic访问EJB所以我宁愿使用no-arg InitialContext()构造函数(并使用 jndi.properties 其他环境的配置文件)但这只是一个侧面说明。然后,你应该查找 com.foo.HelloWorld ,远程接口,而不是 com.foo.HelloWorldBean ,实现:

InitialContext ic = new InitialContext();
(HelloWorld) ic.lookup("HelloWorld#com.foo.HelloWorld");

如果您的bean只有一个远程业务接口,那么这应该有效:

(HelloWorld) ic.lookup("HelloWorld");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top