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

1

6

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.

Feck answered 1/3, 2010 at 21:12 Comment(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?Feck
F
4

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.

Feck answered 3/3, 2010 at 9:53 Comment(3)
Using naming is helpfulI and works fine but does not suit for customization. Any configuration change will impacts your code. EJB jar xml configuration or some configuration injection/proxifying at startup via CDI would do the same, but also enables you to parametirize it, boosting maintenance and deployment capacity.Sanjiv
EJBs that use mapped names may not be portable.Crabby
Chances are applications developed in one proprietary container will never be ported to another brand (as they tend to exploit those containers' extensions.) It's the old dilemma, portability being a requirement for some classes of systems and not for others.Spray

© 2022 - 2024 — McMap. All rights reserved.