Transcript tutorial2

How to Write a Grid Service

The grid service will do basic mathematical operations.
The math grid service is going to have the following
methods:
add:






The input of the method will be an integer value.
The method is going to add the input value to the previous sum.
The initial value will be set to 0.
The method will not return any value.
subtract: This method will work in the same way with
‘add’ operation, as described above. However, it will
subtract the values.
getValue: The method will return the value.
How to Write a Grid Service
 Change your directory to “gt3/samples”.

cd gt3/samples
 Create the following directory structure.
This is actually the package name of the
service.
How to Write a Grid Service

We require four files to deploy a basic service:




A grid service interface file that defines the service
method’s descriptions.
A grid service implementation file that implements
the interface functions.
A WSDD file (Web service deployment descriptor)
that describes the web server how it should publish the
grid service.
A properties file to provide the directory information
of the service.
How to Write a Grid Service
Grid Service Interface




The file name is selected as Math.
It should be placed under
/grid/mathtutorial/core/factory/impl
The interface should include the package name:
mathtutorial.core.factory.impl
The interface name should be as same as the file
name:
public interface Math
How to Write a Grid Service
Grid Service Interface
package mathtutorial.core.factory.impl;
public interface Math
{
public void add(int a);
public void subtract(int a);
public int getValue();
}
How to Write a Grid Service
Grid Service Implementation





The name will be “interface name”+Impl. Hence, here the file name will be
MathImpl. If your interface name is X, the file name would be “XImpl”.
It should be placed under /grid/mathtutorial/core/factory/impl
The interface should include the package name:
mathtutorial.core.factory.impl
The class name will be as same as the file name. It should extend
GridServiceImpl and implements MathPortType.
MathPortType is required for your service communication and will be
generated automatically when you deployed. If your service interface name is
X, then this would be named as “XPortType”. You are expected to include the
following packages for all your services:
import org.globus.ogsa.impl.ogsi.GridServiceImpl;
import mathtutorial.core.factory.Math.MathPortType;
import java.rmi.RemoteException;

You should implement the each interface method in the implementation file.
How to Write a Grid Service
Grid Service Implementation
package mathtutorial.core.factory.impl;
import org.globus.ogsa.impl.ogsi.GridServiceImpl;
import mathtutorial.core.factory.Math.MathPortType;
import java.rmi.RemoteException;
public class MathImpl extends GridServiceImpl implements MathPortType
{
private int value = 0;
public MathImpl()
{
super("Simple Math Factory Service");
}
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;
}
}
How to Write a Grid Service
Grid Service WSDD

One of the key components of the deployment phase is a
file called ‘deployment descriptor’. It’s the file that tells
the web server how it should publish our grid service. The
deployment descriptor is written in WSDD (Web Service
Deployment Descriptor) format.
You should do the following changes in your WSDD file
each time you deploy your service:



Change the service name accordingly:

mathtutorial/core/factory/MathFactoryService
(the directory structure + interface name + “FactoryName”)
Change schema path accordingly:

schema/mathtutorial.core.factory/Math/MathService.wsdl
(“schema” + the directory structure + interface name + interface name
“Service”.wsdl)
The schemaPath tells the grid services container where the WSDL file for this
service can be found.
How to Write a Grid Service
Grid Service WSDD
 Point the baseClassName variable to your
implementation file:

mathtutorial.core.factory.impl.MathImpl
 Such name conventions are required for
automatic deployment. The build.xml file
parses those information, generates all the
communication files (such as stubs) and do
the namings for you.
 The WSDD file should be placed under
mathtutorial/core/factory directory.
How to Write a Grid Service
Grid Service WSDD
<?xml version="1.0"?>
<deployment name="defaultServerConfig" xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="mathtutorial/core/factory/MathFactoryService" provider="Handler" style="wrapped">
<parameter name="name" value="MathService Factory"/>
<parameter name="instance-name" value="MathService Instance"/>
<parameter name="instance-schemaPath"
value="schema/mathtutorial.core.factory/Math/MathService.wsdl"/>
<parameter name="instance-baseClassName" value="mathtutorial.core.factory.impl.MathImpl"/>
<!-- Start common parameters -->
<parameter name="allowedMethods" value="*"/>
<parameter name="persistent" value="true"/>
<parameter name="className" value="org.gridforum.ogsi.Factory"/>
<parameter name="baseClassName" value="org.globus.ogsa.impl.ogsi.PersistentGridServiceImpl"/>
<parameter name="schemaPath" value="schema/ogsi/ogsi_factory_service.wsdl"/>
<parameter name="handlerClass" value="org.globus.ogsa.handlers.RPCURIProvider"/>
<parameter name="factoryCallback" value="org.globus.ogsa.impl.ogsi.DynamicFactoryCallbackImpl"/>
<parameter name="operationProviders" value="org.globus.ogsa.impl.ogsi.FactoryProvider"/>
</service>
.</deployment>
How to Write a Grid Service
Preparing files to build the service


Copy the following files to your grid directory:
cp
/a/juliet/vol/juliet/www/computing/courses/csm23/Tutorial
s/build.* $HOME/gt3/samples/grid/
We are going to modify the “build.properties” file.
ogsa.root=../..
interface.name=Math
package.dir=./mathtutorial/core/factory
package=mathtutorial.core.factory

We need to copy and modify these files accordingly each
time we want to deploy a service.
How to Write a Grid Service
How to Deploy


Change your directory to $HOME/gt3/samples/grid.
Deploy the service:
ant -Djava.interface=true Dpackage=mathtutorial.core.factory Dinterface.name=Math Dpackage.dir=mathtutorial/core/factory/ Dservices.namespace=factory.core.mathtutorial

Before we proceed, goto $HOME/gt3/samples/grid directory and list the
contents of the directory. You will see “build” directory there. Please find the
following files and tell where you found them:




MathPortType.class
MathGridServiceLocator.class
MathImpl.class
Note: To change the directory use “cd”, and to display the contents of the
directory, use “ls” command.
How to Write a Grid Service
How to Deploy

Set the sample directory environment variable.
setenv TUTORIAL_DIR $HOME/gt3/samples/grid

Goto GT3 directory for finalise deployment.
cd $HOME/gt3

We deploy our service.
ant deploy -Dgar.name=$TUTORIAL_DIR/build/lib/mathtutorial.core.factory.Math.gar

Before proceeding, open “build.xml” using pico editor. Find the “deploy”
keyword using CTRL+W command. You should see the following lines:
<target name="deploy">
<ant antfile="${build.packages}" target="deployGar">
<property name="gar.name" value="${gar.name}"/>
</ant>
</target>

Explain what is indicated in these lines.
How to Write a Grid Service
How to Deploy

Go to $HOME/gt3/schema directory and try to
find your package.
cd $HOME/gt3/schema



Did you find it? Can you point where your
MathService.wsdl?
Can you relate it to your WSDD file?
Open the file and explain the section comprising “add”
operation.
<element name="add">
<complexType>
<sequence>
<element name="in0" type="xsd:int"/>
</sequence>
</complexType>
</element>
How to Write a Grid Service
Testing the Grid Service


cd



Start the service container:
globus-start-container –p $MYPORT
Go to your client directory.
$HOME/gt3/samples/grid/mathtutorial/core/factory/client
Then, open a new file called “MathClient.java”.
The package name will be:
mathtutorial.core.factory.client
The following packages should be included in the client
program:
import mathtutorial.core.factory.Math.MathServiceGridLocator;
import mathtutorial.core.factory.Math.MathPortType;
How to Write a Grid Service
Testing the Grid Service
package mathtutorial.core.factory.client;
import mathtutorial.core.factory.Math.MathServiceGridLocator;
import mathtutorial.core.factory.Math.MathPortType;
import java.net.URL;
public class MathClient
{
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.getMathService(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();
}
}
}
How to Write a Grid Service
Testing the Grid Service
cd $HOME/gt3/samples/grid
javac -classpath ./build/classes:$CLASSPATH mathtutorial/core/factory/client/MathClient.java
Create an instance from the service:
ogsi-create-service http://131.227.74.148:9090/ogsa/services/mathtutorial/core/factory/MathFactoryService math1
Run the client program:
java mathtutorial.core.factory.client.MathClient http://131.227.74.148:9090/ogsa/services/mathtutorial/core/factory/MathFactoryService/math1 3
DO NOT FORGET TO CHANGE THE PORT NUMBER AND IP ADDRESS BY LOOKING AT YOUR
SERVICE CONTAINER.
How to Write a Grid Service
Exercises
 Modify the client program to subtract a
value.
 Add multiplication operation to the service.

Note: Before you modify the service, you
should undeploy the service. Because the
changes you made will not take effect.


cd $HOME/gt3
ant undeploy -Dgar.id=mathtutorial