Friday, November 9, 2007

Stateless EJB and client using OC4J 11G

Friday, November 09, 2007 Posted by Unknown ,
In a previous blog I showed a sample of a helloworld stateless ejb on glassfish. In this entry I use the same EJB but I will deploy the bean to the OC4J 11G container. I will use the same client to connect to the OC4J deployed bean. The principles are the same but to connect to the OC4J container we'll need some other libraries and some new config files. Lets start with the deployment of the EJB to the OC4J container:

java -jar $ORACLE_HOME/j2ee/home/admin_client.jar deployer:oc4j:localhost oc4jadmin welcome -deploy -file $HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar -deploymentName HelloEJB

After deploying we get to the client side. First we need the client source.

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat

[sourcecode language='java']

package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.Hello;
public class HelloClient2 {
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
// Piece of code to list all remote JNDI resources
//NamingEnumeration e = ctx.list("");
//while (e.hasMore()) {
// System.out.println(;
Hello h = (Hello)ctx.lookup("HelloBean");
// Will fail because we call the Remote Business interface
// System.out.println(h.sayHelloLocal());
catch (Exception e) {
}[/sourcecode]Notice that by default the lookup needs the Bean name HelloBean.

For OC4J to lookup the InitialContext we need a properties file. This file has to be in the classpath. We use the current directory.

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat

[sourcecode language='java']

[/sourcecode]OC4J also expects a application-client.xml deployment descriptior:

broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient/META-INF$ cat application-client.xml

[sourcecode language='xml']

<?xml version="1.0" encoding="UTF-8"?>

<application-client xmlns=""

<display-name>Client of the earsample</display-name>
<description>client of the earsample</description>

</application-client>[/sourcecode]After this we can compile the client:

javac -cp $ORACLE_HOME/j2ee/home/oc4j.jar:$ORACLE_HOME/j2ee/home/oc4jclient.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar:. -d .

And then run the client:

java -cp $ORACLE_HOME/j2ee/home/oc4j.jar:$ORACLE_HOME/j2ee/home/oc4jclient.jar:$HOME/work/HelloApp/HelloEJB/dist/lib/HelloBean-<date>.jar:. com.bekijkhet.helloclient.HelloClient2

Hello World!!!!
Hello Remote World!!!!