Tuesday, November 20, 2007

Client calls EJB on Glassfish which calls EJB on OC4J 11G

Tuesday, November 20, 2007 Posted by Andre Broers , 4 comments
In this blog I'm showing a way to call an EJB in an OC4J container from an EJB in a Glassfish container.

Let's start with the EJB in the OC4J 11G container:

broersa@debian1:~/work/HelloApp/HelloOrion/src/com/bekijkhet/orion$ cat HelloOrionLocal.java

[sourcecode language="java"]

package com.bekijkhet.orion;
public interface HelloOrionLocal {
  public String sayHello();
  public String sayHelloLocal();
}
[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloOrion/src/com/bekijkhet/orion$ cat HelloOrion.java
[sourcecode language="java"]

package com.bekijkhet.orion;
public interface HelloOrion {
  public String sayHello();
  public String sayHelloRemote();
}
[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloOrion/src/com/bekijkhet/orion$ cat HelloOrionBean.java
[sourcecode language="java"]

package com.bekijkhet.orion;
import javax.ejb.Stateless;
import javax.ejb.Remote;
import javax.ejb.Local;
@Stateless
@Remote(HelloOrion.class)
@Local(HelloOrionLocal.class)
public class HelloOrionBean implements HelloOrion,HelloOrionLocal {
  public String sayHello() {
  return "Hello Orion!!!!";
  }
  public String sayHelloLocal() {
  return "Hello Local Orion!!!!";
  }
  public String sayHelloRemote() {
  return "Hello Remote Orion!!!!";
  }
}[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloOrion$ cat build.xml
[sourcecode language="XML"]


 
  simple example build file
 

 
 
 
 


 
 
 
 
 


  description="compile the source " >
 
 
 


  description="generate the distribution" >
 
 


 
 


  description="clean up" >
 
 
 
 

[/sourcecode]
Type 'asant dist' to create the EJB.Deploy the Bean to the OC4J container:

java -jar $ORACLE_HOME/j2ee/home/admin_client.jar deployer:oc4j:localhost oc4jadmin welcome -deploy -file $HOME/work/HelloApp/HelloOrion/dist/HelloOrion.jar -deploymentName HelloOrion

Now it is time to create the Bean in the Glassfish container which calls the HelloOrion Bean.First we need the Interface from the HelloOrion Bean that we gonna call in the OC4J container. I could include it in a jar file, but for the simplicity I just copy it in the HelloEJB project.

broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet/orion$ cat HelloOrion.java

[sourcecode language="java"]

package com.bekijkhet.orion;
public interface HelloOrion {
  public String sayHello();
  public String sayHelloRemote();
}
[/sourcecode]
Then we need the actual Bean.

broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat Hello.java
[sourcecode language="java"]

package com.bekijkhet;
public interface Hello {
  public String sayHello();
}
[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloEJB/src/com/bekijkhet$ cat HelloBean.java
[sourcecode language="java"]

package com.bekijkhet;
import javax.ejb.Stateless;
import javax.ejb.EJB;
import javax.ejb.Remote;
import java.util.Properties;
import java.net.URL;
import javax.naming.*;
import com.bekijkhet.orion.HelloOrion;
@Stateless
@Remote(Hello.class)
public class HelloBean implements Hello {

static String CONFIG_PROPERTIES = "ejb.properties";

public String sayHello() {
  URL url = null;
  Properties props = null;
  HelloOrion h = null;
  // Use the ClassLoader to get the properties of the Bean to call
  url = Thread.currentThread().getContextClassLoader().getResource(CONFIG_PROPERTIES);
  if (url == null) {
  return "The configuration could not be found: " + CONFIG_PROPERTIES;
  } else {
  props = new Properties();
  try {
  props.load(url.openStream());
  }
  catch (java.io.IOException e) {
  return "Could not read configuration file from URL";
  }
  }
  try {
  Context ctx = new InitialContext(props);
  h = (HelloOrion)ctx.lookup("HelloOrionBean");
  } catch ( Exception e) { return e.toString(); }
  return "Hello From Glassfish!!!! " + h.sayHelloRemote();
  }
}
[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloEJB/properties$ cat ejb.properties
[sourcecode language="XML"]

java.naming.factory.initial=com.evermind.server.rmi.RMIInitialContextFactory
java.naming.provider.url=ormi://localhost/HelloOrion
java.naming.security.principal=oc4jadmin
java.naming.security.credentials=welcome[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloEJB$ cat build.xml
[sourcecode language="XML"]

 
  simple example build file
 

 
 
 
 


 
 
 
 
 


  description="compile the source " >
 
 
 


  description="generate the distribution" >
 
 
 
 
 
 

 



 
 


  description="clean up" >
 
 
 
 

[/sourcecode]
Type asant dist to create the distribution jar.Type asadmin deploy dist/HelloBean.jar to deploy the Bean to glassfish.In this bean I use the classloader to get the properties to call the OC4J bean from a flat file which must be in the Classpath. After this I create a Inital context. To create this context Glassfish needs the $ORACLE_HOME/j2ee/home/oc4jclient.jar in $GLASSFISH/lib directory. After copying this jar you need to restart the Glassfish server.Now it is time to create a simple client to call the EJB in the glassfish container which calls the EJB in OC4J.

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

[sourcecode language="java"]

package com.bekijkhet.helloclient;
import javax.naming.*;
import com.bekijkhet.Hello;
public class HelloClient {
  public static void main(String[] args) {
  try {
  InitialContext ctx = new InitialContext();
  Hello h = (Hello)ctx.lookup("com.bekijkhet.Hello");
  System.out.println(h.sayHello());
  }
  catch (Exception e) {
  System.out.println(e);
  e.printStackTrace();
  }
  }
}[/sourcecode]
javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloEJB/dist/HelloBean.jar:. -d . HelloClient.java

java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$HOME/work/HelloApp/HelloEJB/dist/HelloBean.jar:. com.bekijkhet.helloclient.HelloClient

Hello From Glassfish!!!! Hello Remote Orion!!!!

4 comments: