6
votes

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
Shameless bump. All I've figured out so far is that Weblogic will bind a stateless bean annotated with @Stateless(mappedName="foo") to "foo#fully.qualified.interface.name" in JNDI. But how should I then annotate my helloService field in HelloClientBean? - lindelof

1 Answers

4
votes

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.