3.6. A simple client

We're going to test our web service with a command-line client which will invoke both the add and subtract operations and will also retrieve the Value resource property using the getValueRP operation. This client expects one argument from the command line:

  1. The service URI

The client class will be called Client and we'll place it in the $EXAMPLES_DIR/org/globus/examples/clients/MathService_instance/Client.java file. The full code for the client is the following:

package org.globus.examples.clients.MathService_instance;

import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReferenceType;

import org.globus.examples.stubs.MathService_instance.MathPortType;
import org.globus.examples.stubs.MathService_instance.GetValueRP;
import org.globus.examples.stubs.MathService_instance.service.MathServiceAddressingLocator;

public class Client {

	public static void main(String[] args) {
		MathServiceAddressingLocator locator = new MathServiceAddressingLocator();

		try {
			String serviceURI=args[0];
			
			1
			EndpointReferenceType endpoint = new EndpointReferenceType();
			endpoint.setAddress(new Address(serviceURI));

			2
			MathPortType math = locator.getMathPortTypePort(endpoint);

			3
			// Perform an addition
			math.add(10);

			// Perform another addition
			math.add(5);

			// Access value
			System.out.println("Current value:" + math.getValue(new GetValueRP()));

			// Perform a subtraction
			math.subtract(5);

			// Access value
			System.out.println("Current value:" + math.getValue(new GetValueRP()));
		} catch (Exception e) { 4
			e.printStackTrace();
		}
	}

}
1

First, we create an EndpointReferenceType object representing the endpoint reference of this service. Remember from Section 1.3.2, “The resource approach to statefulness” that an endpoint reference is used to address a particular WS-Resource (the pairing of a service and a resource). Since our service has a single resource, our endpoint reference only needs the service's URI.

2

Next, we obtain a reference to the service's portType. This is done with a stub class called MathServiceAddressingLocator that, given the service's endpoint, returns a MathPortType object that will allow us to contact the Math portType.

3

Once we have that reference, we can work with the web service as if it were a local object. For example, to invoke the remote add operation, we simply have to use the add method in the MathPortType object.

4

Finally, notice how all the code must be placed inside a try/catch block. We must always do this, since all the remote operations can throw RemoteExceptions (for example, if there is a network failure and we can't contact the service).

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.sh 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/stubs/classes/:$CLASSPATH \
org/globus/examples/clients/MathService_instance/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 MathServiceAddressingLocator.

Now, before running the client, we need to to start up the standalone container. Otherwise, our web service won't be available, and the client will crash.

globus-start-container -nosec
[Caution]

We are running the Globus standalone container without any security (-nosec) to avoid having to deal with all the messy security configuration at this point. However, 'real' Grid application will almost always require security. The tutorial currently does not cover security (you will need to refer to the official Globus documentation).

When the container starts up, you'll see a list with the URIs 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/wsrf/services/examples/core/first/MathService
[Note]

This is the service as it would appear in a default GT4 installation, with the standalone container located in http://localhost:8080/wsrf/services. The URI 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/stubs/classes/:$CLASSPATH \
org.globus.examples.clients.MathService_instance.Client \
http://127.0.0.1:8080/wsrf/services/examples/core/first/MathService

If all goes well, you should see the following:

Current value: 15
Current value: 10

Now, remember that our service is, at the same time, the resource itself. So, if we invoke the service repeatedly, we will access the same stateful information. If you run the client a couple more times, you should see the value increase with each run:

Current value: 25
Current value: 20
Current value: 35
Current value: 30