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.

@StatelessDeployment(location="Namaste")
@Stateless
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.

@Stateless

public class HelloWorldBean implements HelloWorld {

@EJBRefMapping(remoteServerRef="true",location="Namaste",

jndiPropertiesFile="META-INF/jndi.properties")

@EJB

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 ");

namaste.sayNamaste(name);

}

}

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:



java.naming.factory.initial=oracle.j2ee.rmi.RMIInitialContextFactory
java.naming.provider.url=ormi://localhost:23792/remoteDI
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome


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 {

@EJB

private static HelloWorld helloWorld;

helloWorld.sayHello(name);

}

Conclusion

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

8 comments:

Anonymous said...

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

Anonymous said...

Does OracleAS/OC4J 10.1.3.2 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 filipe.alves.pinheiro@gmail.com. I'll really apreciate. I've been spent a lot of time on this.

for ict 99 said...

Great ARticle

Java Training in Chennai | Java EE online training

navya said...

Interesting Article

EJB 3 Online Training | Java Online Training

Java Online Training from India | Core Java Online Training