Assignment 1 “Deploying a Simple Web Service” This assignment is

Download Report

Transcript Assignment 1 “Deploying a Simple Web Service” This assignment is

Assignment 1
“Deploying a Simple Web Service”
This assignment is derived from “Classroom Exercises for Grid
Services”
by A. Apon, J. Mache, Y. Yara, and K. Landrus,
Proc. 5th Int. Conference on Linux Clusters: The HPC Revolution
May 2004.
Grid Computing, B. Wilkinson, 2004
A1.1
Task
• To build, deploy and test a simple web
service.
Grid Computing, B. Wilkinson, 2004
A1.1
Tools
This assignment uses:
– Java 2 platform standard edition
– Apache Ant build tool
– Jakarta Tomcat
– Apache Axis tools
Grid Computing, B. Wilkinson, 2004
A1.1
Axis Java Web Service
Facility
• Place a jws (rather than java) file in your
web application directory structure and
axis will automatically find it, compile it,
and deploy the methods.
• For client-side programming see next.
Grid Computing, B. Wilkinson, 2004
A1.1
Steps
• Write the Java code to implement the web
service as a jws file.
• Use Axis to generate all needed Java source
files from the jws file
• Compile client stubs
• Create client source and compile
• Execute client to access service.
• Extend the functionality of the service
Grid Computing, B. Wilkinson, 2004
A1.1
Step 1 – Implement the Service
Using Java write the code for the class that provides
the web service. This code is:
public class Math {
public int squared(int x) {
return x * x;
}
}
Save that code as a .jws (Java Web Service) file,
Math.jws.
Grid Computing, B. Wilkinson, 2004
A1.1
Step 1 (continued)
Copy the .jws file to the axis directory:
cp Math.jws
$CATALINA_HOME/webapps/axis/yourusername
Copying is needed so that the axis tools will be
able to find the .jws file
Grid Computing, B. Wilkinson, 2004
A1.1
Step 2 Generate the WSDL files
Use the Axis tools to create four Java source files from
Math.jws using the command:
java -classpath $AXISCLASSPATH
org.apache.axis.wsdl.WSDL2Java
http://localhost:8080/axis/Math.jws?wsdl
Axis is part of the Apache web server and so the last
argument in the command is a URL indicating where
the new Java files are to be placed
Grid Computing, B. Wilkinson, 2004
A1.1
Step 2 (continued)
•
Axis finds the Math.jws file and creates
two source files each holding a Java interface,
–
–
Math.java and
MathService.java
two source files each holding a Java class,
•
–
–
MathServiceLocator.java
MathSoapBindingStub.java
These files are put in a new package in
localhost/axis/yourusername/Math_jws/
which is in
/home/yourusername/WebServices/
Grid Computing, B. Wilkinson, 2004
A1.1
Step 3 Compile new source files
Compile the source files generated by step 2 with:
javac -classpath $AXISCLASSPATH
localhost/axis/yourusername/Math_jws/*.ja
va
Grid Computing, B. Wilkinson, 2004
A1.1
Step 4: Write Client Source
import
localhost.axis.yourusername.Math_jws.MyMathServiceLocator;
import localhost.axis.yourusername.Math_jws.MyMathService;
import localhost.axis.yourusername.Math_jws.MyMath;
public class MathClient {
public static void main(String args[]) throws Exception {
MathService service = new MathServiceLocator();
Math myMath = service.getMath();
int x = (new Integer(args[0])).intValue();
System.out.println("The square of " + args[0] + " is " +
myMath.squared(x));
}
}
Grid Computing, B. Wilkinson, 2004
A1.1
Step 5 Compile Client code
Compile the client source file with:
javac -classpath $AXISCLASSPATH:. MathClient.java
Grid Computing, B. Wilkinson, 2004
A1.1
Step 6 Execute Web Service program
java -classpath $AXISCLASSPATH MathClient 4
The square of 4 is 16
Axis runs in a servlet container such as Tomcat.
Axis environment executes the server when it is
called.
Grid Computing, B. Wilkinson, 2004
A1.1
Step 7 Extend the Web Service
Add functionality to the Math web service. Add
a method that returns whether a number is
even.
Modify the Math.jws file and repeat all of the
above steps to test the extended web service.
Grid Computing, B. Wilkinson, 2004
A1.1