GlassFish-to-GlassFish Remote EJB Invocation

In this post, I will write about how to invoke EJB between 2 distinct GlassFish instances, that is, the 2 GlassFish servers are not in the same cluster.  The key to accomplishing this is to correctly specify the physical JNDI name of the target remote EJB, which I will explain in more details with code samples.  The following table illustrates the configuration of both the client machine and server machine:

Client Machine Server Machine
 OS Mac OS 10.5.8 Oracle Enterprise Linux
 GlassFish version
GlassFish 4.0 latest trunk build GlassFish 3.1
 Deployed application
 Java SE test program

This sample app consists of 3 artifacts after it is built:

  1. service-ejb.jar: the components that are to be deployed to the remote GlassFish server
  2. client-ejb.jar: the components that are to be deployed to the local GlassFish server
  3. Client.class: the test application main class that interacts directly with the local GlassFish server

The following is a simple sequence diagram showing the interaction:

These source code files are as follows:


package test;
public interface ServiceIF {
public String hello();


package test;
import javax.ejb.*;
public class ServiceBean implements ServiceIF {
public String hello() {
return this.toString();


package test;
public interface ClientIF {
    public String clientHello();


package test;
import javax.ejb.*;
public class ClientBean implements ClientIF {
private ServiceIF serviceBean;
public String clientHello() {
return serviceBean.hello();


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-ejb-jar PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 EJB 3.1//EN" "http://glassfish.org/dtds/glassfish-ejb-jar_3_1-1.dtd">


package test;
import javax.naming.*;
public class Client {
public static void main(String args[]) throws Exception {
InitialContext ic = new InitialContext();
ClientIF clientBean = (ClientIF) ic.lookup(args[0]);
System.out.printf("Result from clientBean.clientHello(): %s%n%n",

To compile all java files:

javac -cp $GLASSFISH_HOME/lib/gf-client.jar test/*java   

To package service-ejb.jar, which is to be deployed to the remote server machine:

jar cvf service-ejb.jar test/ServiceIF.class test/ServiceBean.class

 To package client-ejb.jar, which is to be deployed to the client machine:

jar cvf client-ejb.jar test/ClientIF.class test/ClientBean.class META-INF/glassfish-ejb-jar.xml test/ServiceIF.class

To deploy service-ejb.jar to the remote GlassFish, you need to first transfer service-ejb.jar file to the remote host with ftp or similar tools, then run the following asadmin command in the remote host:

$ [remote-host] ./asadmin start-domain    # if it is not running 
$ [remote-host] ./asadmin deploy $HOME/service-ejb.jar

To deploy client-ejb.jar to the local GlassFish:

./asadmin deploy client-ejb.jar

To run the test Client program, passing in the portable global JNDI name of ClientBean as args:

java -cp $GLASSFISH_HOME/lib/gf-client.jar:. test.Client java:global/client-ejb/ClientBean
Apr 18, 2012 9:36:59 AM com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient
INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default.
Result from clientBean.clientHello(): test.ServiceBean@335aa

The last line of the above output is the result of invoking ServiceBean EJB deployed to the remote GlassFish server.  The INFO message about derby jdbc driver is superfulous and can be ignored for our app.

To undeploy service-ejb from the remote GlassFish, and stop the server:

$[remote-host] ./asadmin undeploy service-ejb
$[remote-host] ./asadmin stop-domain

To undeploy client-ejb from the local GlassFish, and stop the server:

./asadmin undeploy client-ejb
./asadmin stop-domain

There are other ways to specifying the JNDI name of the target remote EJB, besides mapping it in glassfish-ejb-jar.xml:

  • Specify it with lookup or mappedName attribute of @EJB annotation, without the need for any XML descriptor files.  But the drawback is you will need to recompile the code when changing to a different remote host, or different target JNDI name.
  • Specify it in ejb-jar.xml with <lookup-name> or <mapped-name> element.  This is handy especially when you already use ejb-jar.xml for other configurations.  The minor downside is you are mixing runtime environment information along with portable application configuration.

The complete project archive zip can be downloaded here.  More resources that may be of help:

Join the discussion

Comments ( 1 )
  • guest Friday, April 20, 2012



    Corba? Really?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha

Integrated Cloud Applications & Platform Services