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:
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]; EndpointReferenceType endpoint = new EndpointReferenceType(); endpoint.setAddress(new Address(serviceURI)); MathPortType math = locator.getMathPortTypePort(endpoint); // 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) { e.printStackTrace(); } } }
First, we create an | |
Next, we obtain a reference to the service's portType. This is done with a stub class called | |
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 | |
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 |
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
We are running the Globus standalone container without any security ( |
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
This is the service as it would appear in a default GT4
installation, with the standalone container located in
|
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