This section will show you how to build with FraSCAti a client of
a weather web service. The weather web service is already deployed over
Internet. The source code for this example can be found in the
examples/weather
folder of the distribution or in
the trunk/examples/weather
of the OW2 FraSCAti SVN repository .
This example use the web service provided at
http://www.webservicex.net/globalweather.asmx. It offers a
GetWeather
operation which provides weather information
for the requested city. The service interface is defined by a WSDL
document which can be retrieved in the
src/main/wsdl
folder or at
http://www.webservicex.net/globalweather.asmx?wsdl
In this example we generate the Java interface corresponding to
the provided WSDL. This Java interface is later used by the Java
client implementation in order to perform service call. To generate
the weather service java interface we use the Apache CXF WSDL2Java
tool, available from the frascati
command,. The WSDL2Java
execution on the 'globalweather' WSDL result in a set of java file
where net.webservicex.GlobalWeatherSOAP
is the java
interface for the weather web service.
We will now describe how to implement the SCA java client
component of the weather web service. The SCA client is a small
component which request the user for country and city name, and print
current weather for this location. The implementation of the
org.ow2.frascati.examples.weather.Client
is given below
:
package org.ow2.frascati.examples.weather; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import net.webservicex.GlobalWeatherSoap; import org.osoa.sca.annotations.Reference; /** * SCA Java Component implementation of a weather client */ public class Client implements Runnable { /** Reference on the provided weather service */ @Reference protected GlobalWeatherSoap weather; /** Run method for the weather client component */ public void run() { try { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); /** Request for country name */ System.out.println("\nWeather service"); System.out.println("\nEnter Country Name :"); String country = input.readLine(); /** Request for city name */ System.out.println("\nEnter City Name :"); String city = input.readLine(); /** Print 'waiting' message */ System.out.println("\n\nGetting weather description for " + city + ", " + country + ".......\n\n"); /** Call the weather service for the given location */ String result = weather.getWeather(city, country); /** Print weather details returned by the service */ System.out.println(result+"\n\n"); } catch (IOException e) { throw new Error("Error when calling the weather service",e); } } }
The weather client implements the
java.lang.Runnable
interface. This interface is exposed
as an SCA runnable service by the weather composite.
You can now test by yourself the FraSCAti weather example. For
that, go in the $FRASCATI_HOME/examples/weather
directory, then perform the following steps:
Generate the Java interfaces from the WSDL definition:
$ frascati wsdl2java -u http://www.webservicex.net/globalweather.asmx?wsdl -o src/generated/java
FraSCAti: generating Java from WSDL...
WSDL file: src/main/wsdl/globalweather.asmx-WSDL.wsdl
output directory : src/main/java
Java code successfully generated!
Implement and compile the business code of your client, by using
the generated interface
(net.webservicex.GlobalWeatherSoap
). This code is
available in the sources of the example or here.
$ frascati compile src weather
Compiling ...
src
weather
Library weather.jar created!
This command requires the javax.ws.*
libraries
provided since Java 6. For running this example with Java 5, add to
your classpath the geronimo-ws-metadata_2.0_spec-1.1.2
jar, available in $FRASCATI_HOME/lib
.
Then run the example, using the following command:
$ frascati run weather -libpath weather.jar -s r -m run