A simple client

We're going to test our Grid Service with a command-line client which will invoke the add method and the getValue method. This client will receive two arguments from the command line:

  1. The Grid Service Handle (GSH)

  2. Number to add

The client class will be called Client and we'll place it in the $TUTORIAL_DIR/org/globus/progtutorial/clients/MathService/Client.java file. Again, you can find more information about the directory structured followed in the tutorial in the Tutorial Directory Structure appendix.

The full code for the client is the following:

package org.globus.progtutorial.clients.MathService;

import org.globus.progtutorial.stubs.MathService.service.MathServiceGridLocator;
import org.globus.progtutorial.stubs.MathService.MathPortType;

import java.net.URL;

public class Client
{
  public static void main(String[] args)
  {
    try
    {
      // Get command-line arguments
      URL GSH = new java.net.URL(args[0]);
      int a = Integer.parseInt(args[1]);

      
      // Get a reference to the MathService instance
      MathServiceGridLocator mathServiceLocator = new MathServiceGridLocator();
      MathPortType math = mathServiceLocator.getMathServicePort(GSH);

      // Call remote method 'add'
      math.add(a);
      System.out.println("Added " + a);

      // Get current value through remote method 'getValue'
      int value = math.getValue();
      System.out.println("Current value: " + value);
    }catch(Exception e)
    {
      System.out.println("ERROR!");
      e.printStackTrace();
    }
  }
}
[Note]

This file is $TUTORIAL_DIR/org/globus/progtutorial/clients/MathService/Client.java

As you can see, writing a Grid Service client is very easy. With only two lines we obtain a reference to the Math portType. In following sections, as we introduce things like service data, notifications, and factories the code to obtain that reference will be slightly longer. However, the important point is that, once we have that reference, we can work with the Grid Service as if it were a local object. Notice, however, that we have to put the whole code inside a try/catch block, because the Grid Service methods (in this example, the add method) can throw RemoteExceptions.

We are now going to compile the client. Before running the compiler, make sure you run the following:

source $GLOBUS_LOCATION/etc/globus-devel-env.sh

The globus-devel-env script takes care of putting all the Globus libraries into your CLASSPATH. When compiling the service, Ant took care of this but, since we're not using Ant to compile the client, we need to run the script.

To compile the client, do the following:

javac \
-classpath ./build/classes/:$CLASSPATH \
org/globus/progtutorial/clients/MathService/Client.java

./build/classes is a directory generated by Ant were all the compiled stub classes are placed. We need to include this directory in the Classpath so our client can access generated stub classes such as MathServiceGridLocator. Before running it, we need to to start up the standalone container. Otherwise, our Grid Service won't be available, and the client will crash. The following command must be run from the root of your GT3 installation:

globus-start-container
[Note]

You need to setup the GT3 command-line clients for this command to work. If you have no idea what I'm talking about, take a look at the following section of the How to... appendix: How to setup the GT3 scripts.

When the container starts up, you'll see a list with the GSH of all the deployed services. One quick way of checking if MathService has been correctly deployed is to check if the following line appears in the list of services:

http://127.0.0.1:8080/ogsa/services/progtutorial/core/first/MathService

This is the service as it would appear in a default GT3 installation, with the standalone container located in http://localhost:8080/ogsa/services. The GSH might be different if you've changed the location of the container. If the service is correctly deployed, we can now run the client:

java \
-classpath ./build/classes/:$CLASSPATH \
org.globus.progtutorial.clients.MathService.Client \
http://127.0.0.1:8080/ogsa/services/progtutorial/core/first/MathService \
5

If all goes well, you should see the following:

Added 5
Current value: 5

This means we've successfully added 5 to the internal value of the service (which is initially zero). To see how Grid Services are truly stateful, you can run the client again. You should see the following:

Added 5
Current value: 10

This, of course, means that after adding 5 to the internal value, which was previously equal to 5, the internal value is now equal to 10.

Voila! You are now one step closer to the Grid Service Nirvana! :-)