5.8. A simple client

We will try out our service first with a simple client that creates a new resource and performs a couple operations on it. This client expects only one argument from the command line, the factory service's URI

package org.globus.examples.clients.FactoryService_Math;

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

import org.globus.examples.stubs.MathService_instance.GetValueRP;
import org.globus.examples.stubs.MathService_instance.MathPortType;
import org.globus.examples.stubs.MathService_instance.service.MathServiceAddressingLocator;
import org.globus.examples.stubs.Factory.service.FactoryServiceAddressingLocator;
import org.globus.examples.stubs.Factory.FactoryPortType;
import org.globus.examples.stubs.Factory.CreateResource;
import org.globus.examples.stubs.Factory.CreateResourceResponse;

/* This client creates a new MathService instance through a FactoryService. This client
 * expects one parameter: the factory URI.
 */
public class Client {

	public static void main(String[] args) {
		FactoryServiceAddressingLocator factoryLocator = new FactoryServiceAddressingLocator();
		MathServiceAddressingLocator instanceLocator = new MathServiceAddressingLocator();

		try {
			String factoryURI = args[0];
			EndpointReferenceType factoryEPR, instanceEPR;
			FactoryPortType mathFactory;
			MathPortType math;

			1
			factoryEPR = new EndpointReferenceType();
			factoryEPR.setAddress(new Address(factoryURI));
			mathFactory = factoryLocator.getFactoryPortTypePort(factoryEPR);

			2
			CreateResourceResponse createResponse = mathFactory
					.createResource(new CreateResource());
			instanceEPR = createResponse.getEndpointReference();

			3
			math = instanceLocator.getMathPortTypePort(instanceEPR);

			System.out.println("Created instance.");

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

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

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

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

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

}

[Note]

This file is $EXAMPLES_DIR/org/globus/examples/clients/FactoryService_Math/Client.java.

1

Here we obtain a reference to the factory's portType. Notice how we only need the factory's URI to do this.

2

Once we have the factory's portType, we use it to invoke the createResource operation. This operation returns an endpoint reference, 'boxed' inside a CreateResourceResponse object. This endpoint reference includes both the instance service's URI and the new resource's identifier. In the next client we will take a peek inside the endpoint reference.

3

Using the instance EPR, we obtain a reference to the MathPortType in the instance service.

4

We now use the MathPortType to invoke add, subtract, and getValueRP.

Compile and run the client:

javac \
-classpath ./build/stubs/classes/:$CLASSPATH \
org/globus/examples/clients/FactoryService_Math/Client.java
java \
-classpath ./build/stubs/classes/:$CLASSPATH \
org.globus.examples.clients.FactoryService_Math.Client \
http://127.0.0.1:8080/wsrf/services/examples/core/factory/MathFactoryService

If all goes well, you should see the following:

Created instance.
Current value:15
Current value:10

If you run it again, you will get the exact same result. This is because we are creating a new resource every time we run the client.

Created instance.
Current value:15
Current value:10