Wednesday, May 09, 2007

Using Dependency Injection to invoke EJB deployed in a remote container

Many applications require invocation of EJBs deployed in a remote container. EJB 3 and Java EE 5 support injection of remote EJB references only in the application client container. Let us say you want to invoke an EJB based on a remote container from your web client (JSF backing bean) or an EJB then you have to resort to the JNDI lookup. Unfortunately injection of remote references have been left as an extension to application server vendors.

In this blog I will demonstrate how OC4J 11g preview supports injection of EJB references that is deployed in a remote container.

Let us assume that we have two Session EJBs HelloWorld and Namaste deployed in two different OC4J instances. The HelloWorld EJB uses dependency injection to get a reference to Namaste EJB located in a remote container.

Stateless Session Bean example using EJB 3.0

We will deploy Namaste EJB into second OC4J instance that will be remotely invoked by the HelloWorld EJB. The following is the coded for the Namaste EJB.

public class NamasteBean implements Namaste 
public void sayHello(String name)
System.out.println("Namaste "+name +" from Remote EJB");

Note that the location parameter of @oracle.j2ee.annotation.ejb.StatelessDeployment is used to configure global JNDI name for the Namaste EJB.

Injection Code to Invoke the Remote EJB

OC4J allows injecting remote EJB references (deployed on remotre server) by using a proprietary annotation @oracle.j2ee.annotation.ejb.EJBRefMapping, as shown in the following code. Note that HelloWorld EJB is deployed in OC4J instance 1.


public class HelloWorldBean implements HelloWorld {




private Namaste namaste;

public void sayHello(String name) {

System.out.println("Hello " + name+ " from your first EJB 3.0 component ... ");

System.out.println("Using Injection to invoke method of Namaste Bean! See console of remote container ");




The location element specifies the global JNDI for the remote interface of the EJB and jndiProperties file specifies the JNDI properties such as provider URL, principal, credential, etc to look up the remote EJB as follows:


You can use @EJBRefMapping annotation in any manage classes in the container such as Servlet, listeners or managed POJOs. If you are not comfortable using Oracle's extension, you can use the equivalent in oracle-ejb-jar.xml.

Client Code

The client code runs in Oracle’s application client container and uses dependency injection to invoke methods on HelloWorld EJB as follows:.

public class HelloWorldClient {


private static HelloWorld helloWorld;




Dependency Injection of remote EJB references in not standardized. You can use Oracle's proprietary extension to injection remote EJB references.


Anonymous said...

How can I make the same thing using BEA Weblogic Server 10.0?

Anonymous said...

Does OracleAS/OC4J support the same thing?

Anonymous said...

Hi Panda! Excelent article! This feature is very hidden in the docs.

Do you know if is it possible to make something similar with glassfish or jboss?

Best regards.

Anonymous said...

Nice article and wonderful explanation. Thank you very much...

Anonymous said...

Panda, thank you for the article. Do you know if is it possible to make something similar with glassfish or jboss?

Anonymous said...

Panda, thank you for the article. I need to know the same as the guys above. Do you know if is it possible to make something similar with glassfish or jboss? If you or someone else can help me please send me e-mail to I'll really apreciate. I've been spent a lot of time on this.

for IT the said...

Great ARticle

Java Training in Chennai | Java EE online training

Naviya Nair said...

Interesting Article

EJB 3 Online Training | Java Online Training

Java Online Training from India | Core Java Online Training