Orchestration with JSR181
This tutorial will explain how you can leverage the servicemix-jsr181 component to orchestrate web services. We will use two public web services:
We will call them in a simple way to provide an aggregate web service which will return the weather forecast for a given city / state and expose it through as an HTTP/SOAP service.
For this tutorial, you will need a 3.1-incubating-SNAPSHOT version of ServiceMix built from sources.
Project structure, SUs and SA
In this example, we will only use two components:
- servicemix-jsr181 for the orchestration part
- servicemix-http to expose and consume services through HTTP/SOAP
Thus we will have to create two service units and a service assembly.
First, we need to create the root maven project which will hold our SUs and SA. Launch the following commands:
mkdir weather cd weather
And create a file named pom.xml
with the following content:
<project> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.servicemix.samples</groupId> <artifactId>weather</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> </project>
Then, we can use maven archetypes to create the two SUs and the SA. In the weather
directory, launch the following commands:
cd weather mvn archetype:create \ -DarchetypeGroupId=org.apache.servicemix.tooling \ -DarchetypeArtifactId=servicemix-http-consumer-service-unit \ -DarchetypeVersion=3.1-incubating-SNAPSHOT \ -DgroupId=org.apache.servicemix.samples.weather \ -DartifactId=weather-http-su mvn archetype:create \ -DarchetypeGroupId=org.apache.servicemix.tooling \ -DarchetypeArtifactId=servicemix-jsr181-wsdl-first-service-unit \ -DarchetypeVersion=3.1-incubating-SNAPSHOT \ -DgroupId=org.apache.servicemix.samples.weather \ -DartifactId=weather-jsr181-su mvn archetype:create \ -DarchetypeGroupId=org.apache.servicemix.tooling \ -DarchetypeArtifactId=servicemix-service-assembly \ -DarchetypeVersion=3.1-incubating-SNAPSHOT \ -DgroupId=org.apache.servicemix.samples.weather \ -DartifactId=weather-sa
This will create the following directory structure:
bridge\ pom.xml weather-http-su\ ... weather-jsr181-su\ ... weather-sa\ ...
Generating Eclipse projects
Now that we have the projects created, be can import them in Eclipse. Run the following command to build the eclipse project files:
cd weather mvn eclipse:eclipse
Now, we can import the projects in Eclipse.
The jsr181 SU
The first thing to do is to design the WSDL that will be exposed as a service, so that we can generate the needed classes and implement the service.
Edit the weather/weather-jsr181-su/src/main/resources/service.wsdl
and replace it by the following one:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Weather" targetNamespace="http://servicemix.apache.org/samples/weather" xmlns:tns="http://servicemix.apache.org/samples/weather"> <wsdl:types> <xsd:schema targetNamespace="http://servicemix.apache.org/samples/weather" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://servicemix.apache.org/samples/weather"> <xsd:element name="GetWeatherRequest"> <xsd:complexType> <xsd:sequence> <xsd:element name="City" type="xsd:string"></xsd:element> <xsd:element name="State" type="xsd:string"></xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="GetWeatherResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="string" type="xsd:string" minOccurs="0" maxOccurs="unbounded"> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="GetWeatherRequest"> <wsdl:part name="GetWeatherRequest" element="tns:GetWeatherRequest"> </wsdl:part> </wsdl:message> <wsdl:message name="GetWeatherResponse"> <wsdl:part name="GetWeatherResponse" element="tns:GetWeatherResponse"> </wsdl:part> </wsdl:message> <wsdl:portType name="WeatherPortType"> <wsdl:operation name="GetWeather"> <wsdl:input message="tns:GetWeatherRequest"></wsdl:input> <wsdl:output message="tns:GetWeatherResponse"></wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="WeatherSoapBinding" type="tns:WeatherPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="GetWeather"> <soap:operation soapAction="http://servicemix.apache.org/samples/weather/GetWeather" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="WeatherService"> <wsdl:port name="Soap" binding="tns:WeatherSoapBinding"> <soap:address location="http://www.example.org/" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
Then, grab the WSDL definitions for the two web services we will use. On Unix systems, you can use:
cd weather/weather-jsr181-su/src/main/resources/ wget http://ws.strikeiron.com/InnerGears/ForecastByZip2?WSDL mv ForecastByZip2\@WSDL ForcastByZip2.wsdl wget http://ws.strikeiron.com/InnerGears/ZipByCityState2?WSDL mv ZipByCityState2\@WSDL ZipByCityState2.wsdl
If you use Windows, just download from them your web browser and put them in the above directory.
Then, we need to modify the pom.xml
file to generate the classes for the web services. The archetype we used already has a definition for the main WSDL, we just need to copy it for the two new WSDLs:
<wsgen outputDirectory="${basedir}/target/generated-sources" explicitAnnotation="true" profile="org.codehaus.xfire.jaxws.gen.JAXWSProfile" wsdl="${basedir}/src/main/resources/service.wsdl"></wsgen> <wsgen outputDirectory="${basedir}/target/generated-sources" explicitAnnotation="true" profile="org.codehaus.xfire.jaxws.gen.JAXWSProfile" wsdl="${basedir}/src/main/resources/ForecastByZip2.wsdl"></wsgen> <wsgen outputDirectory="${basedir}/target/generated-sources" explicitAnnotation="true" profile="org.codehaus.xfire.jaxws.gen.JAXWSProfile" wsdl="${basedir}/src/main/resources/ZipByCityState2.wsdl"></wsgen>
Now, generate all the classes from these three WSDLs by launching:
cd weather/weather-jsr181-su mvn generate-sources