如何在另一个EAR中调用远程EJB?

lin*_*lof 6 ejb weblogic java-ee ejb-3.0

在Weblogic 10.3中,如何将远程EJB从一个EAR注入到另一个EAR的无状态bean中,两个EAR是否都部署在同一个容器中?理想情况下,我想尽可能多地使用注释.

所以假设我有以下界面:

public interface HelloService {
  public String hello();
}
Run Code Online (Sandbox Code Playgroud)

由以下EJB实现:

@Stateless
@Remote
public class HelloServiceBean implements HelloService {
  public String hello() {
      return "hello";
  }
}
Run Code Online (Sandbox Code Playgroud)

假设它们已经打包并部署在中server.ear.现在client.ear,我有以下内容:

@Stateless
public class HelloClientBean {
    @EJB
    HelloService helloService;

// other methods...
}
Run Code Online (Sandbox Code Playgroud)

我需要添加什么才能让Weblogic在HelloClientBeanin client.earHelloServiceBeanin 之间正确地计算出布线server.ear?热烈欢迎官方文件和/或书籍的指示.

lin*_*lof 4

到目前为止我发现的最简单的解决方案如下。

首先,用属性注释无状态 bean mappedName

@Stateless(mappedName="HelloService")
@Remote
public class HelloServiceBean implements HelloService {
  public String hello() {
      return "hello";
  }
}
Run Code Online (Sandbox Code Playgroud)

根据http://forums.oracle.com/forums/thread.jspa?threadID=800314&tstart=1,Weblogic永远不会为EJB创建JNDI条目,除非给出JNDI名称作为属性mappedName(或者在部署描述符中,或在专有注释中)。

@EJB接下来,您现在可以使用属性来注释您的客户端字段mappedName,该属性应该与服务器 bean 上的属性相同。(老实说,我对此感到困惑。在 Weblogic 10.3 中调用 EJB 时出现 NameNotFoundException表明我应该能够使用该mappedName#interfaceName语法,但在我的测试中这不起作用。):

@Stateless
public class HelloClientBean {
    @EJB(mappedName="HelloService")
    HelloService helloService;

// other methods...
}
Run Code Online (Sandbox Code Playgroud)

现在,只要两个 EAR 都部署在同一个容器中,就可以了。接下来我将尝试找出将它们部署在不同机器上时的正确语法。