After defining the service interface ("what the service does"), the next step is implementing that interface. The implementation is "how the service does what it says it does". The implementation of a Grid Service is simply a Java class which, besides implementing the operations described in the GWSDL file, has to meet certain requirements. This class can furthermore include additional private methods which won't be available through the Grid Service interface, but which our service can use internally.
We'll implement the service in a Java class called MathImpl which we'll place in $TUTORIAL_DIR/org/globus/progtutorial/services/core/first/impl/MathImpl.java. If you're wondering why we're saving it in that particular spot, take a look at the Tutorial Directory Structure appendix. However, if you don't completely grasp the directory structure right now, don't worry. You can safely take a leap of faith right now, save the files where the tutorial tells you to and, after you've done a couple of examples, take a look at the Tutorial Directory Structure and see how everything fits together.
So, let's start coding! The first thing we'll do in the Java file is include the package declaration, and import a couple of classes we'll need (and which will be described shortly).
package org.globus.progtutorial.services.core.first.impl; import org.globus.ogsa.impl.ogsi.GridServiceImpl; import org.globus.progtutorial.stubs.MathService.MathPortType; import java.rmi.RemoteException;
Now we'll declare the MathImpl class, which will be the implementation of our Grid Service.
public class MathImpl extends GridServiceImpl implements MathPortType
Notice the following:
MathImpl is a child class of GridServiceImpl. All Grid Services must extend from the base class GridServiceImpl. This is what is usually called the skeleton class, because it contains the 'bare bones' -the basic functionality- common to all Grid Services. In fact, GridServiceImpl implements all the operations declared in the GridService portType which all Grid Services must extend from (as described in What is a Grid Service?)
Our Grid Service implements an interface named MathPortType. Remember that PortType is another name for "service interface". We're telling Java that this Grid Service implements a particular portType: the Math PortType. But, where did MathPortType come from? This is one of the stub files which will be generated from the GWSDL file once we compile the service..
Next, we have to write a constructor for our Grid Service. Our constructor will simply call the GridServiceImpl constructor, which expects a String with a description of the Grid Service.
public MathImpl() { super("Simple Math Service"); }
Next, remember we said in the previous page that we want MathService to 'remember' an internal value which we'll add to and subtract from using the add and subtract methods. We simply have to declare a private integer attribute:
private int value = 0;
Finally, we implement the methods specified in the service interface: add, subtract, and getValue. Notice how, despite being very simple methods, they can all throw a RemoteException. Since they are remote methods (methods which can be accessed remotely, through the Grid Service), a RemoteException can be thrown if there is a problem between the server and the client (for example, if there is a network error).
public void add(int a) throws RemoteException { value = value + a; } public void subtract(int a) throws RemoteException { value = value - a; } public int getValue() throws RemoteException { return value; }
With that, we have finished implementing our Grid Service. Now we only have a couple of simple steps before we can finally see it work!
The complete code for the implementation is shown here:
package org.globus.progtutorial.services.core.first.impl; import org.globus.ogsa.impl.ogsi.GridServiceImpl; import org.globus.progtutorial.stubs.MathService.MathPortType; import java.rmi.RemoteException; public class MathImpl extends GridServiceImpl implements MathPortType { private int value = 0; public MathImpl() { super("Simple MathService"); } public void add(int a) throws RemoteException { value = value + a; } public void subtract(int a) throws RemoteException { value = value - a; } public int getValue() throws RemoteException { return value; } }
This file is $TUTORIAL_DIR/org/globus/progtutorial/services/core/first/impl/MathImpl.java |