Remote Accessibility EJB 3.1 – Part 2

In this part I’ll show you how connect a client application to application server to access a remote object distributed.

You’ll see how consuming the object from client and test it.

In the previous part we’ve looked at the server side part. Now it’s time to build the consumer object.

@EJB vs Initial Context

First , I must describe the difference between using @EJB annotation and “InitialContext” lookup approach. Both method are used to define the remote object into our client class. I’ve looked at many example over internet that used either @EJB annotation or Context.

What’s the differences? Easily, you can use @EJB annotation if you are running your code into an application server. This is possible, for example, into a Web application site or application console.

For this last enviroment execution, in glassfish, you have to use ${glassfish.home}/bin/appclient file to sun you application console into application server.

Are you confused? Yes, me too.

The main problem that I’ve found is that, in this way, I’ve not been able to debug a console client application with @EJB annotation into eclipse.

In this thread you can look at the problem about:

In the follow examples I used both application console and JUnit test for showing you the differents method. I remind you to include gf-client.ajr into libraries of the project. 

Console Client

The console client code:

package it.orderprojectserverclient.client;

import javax.ejb.EJB;
import javax.naming.NamingException;

import it.orderprojectserver.bean.Order;
import it.orderprojectserver.controller.IOrder;

public class OrderClient {
    private static IOrder bean;
 public static void main(String args[]) throws NamingException {

  Order order = new Order("CH-2511", 10, 0);
  order = bean.processOrder(order);
  order = bean.shippingOrder(order);


To compile it you must add the reference to Order bean and IOrder remote object from server project.

To run it I’ve found easy to build a short ant build file (remind to run application server before):

<target name="runappclient"
        description="execute Application Client">
  <exec executable="${glassfish.home}/bin/appclient"
    <arg line="-client ${build}/Orderappclient.jar"/>

If you are lucky the result should be this:

[exec] "C:\PROGRA~1\Java\jre7\bin\java.exe" -Dcom.sun.aas.installRoot="C:\glassfish3\glassfish\bin\..""C:\glassfish3\glassfish\bin\..\lib\appclient\client.policy" -Djava.system.class.loader=org.glassfish.appclient.client.acc.agent.ACCAgentClassLoader"C:\glassfish3\glassfish\bin\..\lib\appclient\appclientlogin.conf"  -javaagent:"C:\glassfish3\glassfish\bin\..\lib\gf-client.jar"=argsfile="C:\DOCUME~1\MARCOG~1\LOCALS~1\Temp\acc2858317377610925967.dat" -Djava.ext.dirs="C:\glassfish3\glassfish\bin\..\lib\ext";"C:\PROGRA~1\Java\jre7\lib\ext" -Djava.endorsed.dirs="C:\glassfish3\glassfish\bin\..\lib\endorsed";"C:\glassfish3\glassfish\bin\..\modules\endorsed";"C:\PROGRA~1\Java\jre7\lib\endorsed"  -jar "C:\eclipse-A2\workspace\OrderProcessClient\build/Orderappclient.jar"

[exec] orderId: CH-2511 quantity: 10 amount: 8.0 response:Shipping in time Mon Jun 18 12:30:49 CEST 2012


As a good developer would say, every class should have own unit test class. So, this is our unit test class.

package it.orderprojectserverclient.test;
import static org.junit.Assert.*;
import it.orderprojectserver.bean.Order;
import it.orderprojectserver.controller.IOrder;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.junit.Before;
import org.junit.Test;

public class OrderTest {

 public String message=new String();
 static InitialContext ctx;
 static IOrder bean;
 public void setup() throws NamingException
  ctx = new InitialContext();
  bean = (IOrder) ctx.lookup("ejb/OrderProjectServer");
 public void verifyOrder() throws NamingException
  Order order = new Order("CH-2511", 10, 0);
  order = bean.processOrder(order);
  order = bean.shippingOrder(order);

I could have used the annotation approach even in this code. My aim was to debug it in eclipse IDE, and I haven’t still found the way to do it (if anybody knows it, please, tell me how can I do it.)

As we’ve seen before, the result should be green..

This example works if the server and client are in the same machine. How can I access it if the client and server are in different machines? Here the answer (at least for glassfish).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s