Friday, November 9, 2007

Stateless EJB and client using OC4J 11G

Friday, November 09, 2007 Posted by Andre Broers , 2 comments
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 HelloClient2.java

[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(e.next());
//}
Hello h = (Hello)ctx.lookup("HelloBean");
System.out.println(h.sayHello());
System.out.println(h.sayHelloRemote());
// Will fail because we call the Remote Business interface
// System.out.println(h.sayHelloLocal());
}
catch (Exception e) {
e.printStackTrace();
}
}
}[/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 jndi.properties

[sourcecode language='java']
java.naming.factory.initial=com.evermind.server.ApplicationClientInitialContextFactory
java.naming.provider.url=ormi://localhost/HelloEJB
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome

[/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="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd"
version="1.4">

<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 . HelloClient2.java

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!!!!

2 comments: