How do I call a remote EJB in an EAR from another?

自作多情 提交于 2019-12-07 01:58:53

问题


In Weblogic 10.3, how do I inject a remote EJB from one EAR into a Stateless bean of another, both EARs being deployed in the same container? Ideally I'd like to do as much as possible with annotations.

So suppose I have the following interface:

public interface HelloService {
  public String hello();
}

implemented by the following EJB:

@Stateless
@Remote
public class HelloServiceBean implements HelloService {
  public String hello() {
      return "hello";
  }
}

Suppose they're packaged and deployed in server.ear. Now in client.ear, I have the following:

@Stateless
public class HelloClientBean {
    @EJB
    HelloService helloService;

// other methods...
}

What do I need to add so that Weblogic figures out the wiring correctly between HelloClientBean in client.ear and HelloServiceBean in server.ear? Pointers to official documentations and/or books warmly welcome.


回答1:


The easiest solution I've found so far is the following.

First, annotate the stateless bean with a mappedName attribute:

@Stateless(mappedName="HelloService")
@Remote
public class HelloServiceBean implements HelloService {
  public String hello() {
      return "hello";
  }
}

According to http://forums.oracle.com/forums/thread.jspa?threadID=800314&tstart=1, Weblogic will never create a JNDI entry for an EJB unless a JNDI name is given as the mappedName attribute (or in the deployment descriptor, or in a proprietary annotation).

Next, you can now annotate your client field with @EJB with a mappedName attribute, which should be the same as the attribute on the server bean. (I am honestly baffled by this. NameNotFoundException when calling a EJB in Weblogic 10.3 suggests that I should be able to use the mappedName#interfaceName syntax, but in my tests this doesn't work.):

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

// other methods...
}

This now works, as long as both EARs are deployed in the same container. Next I'll try to figure out the right syntax when they are deployed on different machines.



来源:https://stackoverflow.com/questions/2359109/how-do-i-call-a-remote-ejb-in-an-ear-from-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!