Mule : Stock Quote Example
This page last changed on Oct 02, 2006 by lajos@mulesource.com.
This example demonstrates how to invoke an ASPX web service from Mule and transform the result using XSLT and deserialise the result to a StockQuote javabean. The example demonstrates using REST and SOAP to invoke the service. The configuration uses the System stream connector to receive a stock symbol from a user (System.in), invokes the StockQuote service, transforms the result using the XSLT transformer and then uses the XmlToObject transformer to convert the result into a StockQuote javabean. Finally the Quote is sent to the output console (System.out). Running the ApplicationPrerequisitesTo run you will need the following -
Optional
Running
- Walk ThroughThis walkthough will use the REST configuration for demonstration, but the SOAP configuration is very similar. First the transformers need to be configured - <transformers> <transformer name="XmlToObject" className="org.mule.transformers.xml.XmlToObject"/> <transformer name="SgmlDecoder" className="org.mule.transformers.codec.SgmlEntityDecoder"/> <transformer name="Xslt" className="org.mule.transformers.xml.XsltTransformer"> <properties> <property name="xslFile" value="stock.xsl"/> </properties> </transformer> </transformers> There are 3 transformers, XmlToObject, SgmlDecoder and Xslt. The Xslt transformer requires that you set the xslFile to a resource located on the file system on classpath. These transformers are chained together. The stock quote service uses the REST Service Wrapper component that Allows REST services to be proxied to act like local Mule services. <mule-descriptor name="RESTServiceWrapper" implementation="org.mule.components.rest.RestServiceWrapper"> <inbound-router> <endpoint address="stream://System.in"/> </inbound-router> <outbound-router> <router className="org.mule.routing.outbound.FilteringOutboundRouter"> <endpoint address="stream://System.out" transformers="SgmlDecoder Xslt XmlToObject"/> </router> </outbound-router> <properties> <property name="serviceUrl" value="http://www.webservicex.net/stockquote.asmx/GetQuote"/> <property name="payloadParameterName" value="symbol"/> <property name="httpMethod" value="POST"/> <property name="errorExpression" value="[^<.*]"/> </properties> </mule-descriptor> The component receives stock symbols from System.in and will output the result to System.out. Notice the transformers set on the outbound endpoint. Mule allows you to combine multiple transformers into a transform pipeline. The RestServiceWrapper has a number of properties configured. The serviceUrl is the URL of the REST service to invoke. The payloadParameterName is the name of the parameter to use set the message payload on the serviceUrl. The httpMethod can either be GET or POST. Finally the errorExpression is a Regular Expression that is applied to the result of the service invocation to determine if an error occurred. In this case the StockQuote service returns a non-xml error string. The Soap VersionThe difference in the two methods is in the configuration. The SOAP way of doing things in Mule differs in that We now use a Bridge Component that takes the input from one endpoint and passes it directly to the outbound router, and we an Axis outbound endpoint configured with Named Parameters that map to the Stock Quote service. <mule-descriptor name="serviceProxy" implementation="org.mule.components.simple.BridgeComponent"> <inbound-router> <endpoint address="stream://System.in"/> </inbound-router> <outbound-router> <!-- we use a chaining transformer to send the results of one endpoint execution as the input for the next endpoint. In this case it writes it out to System.out --> <router className="org.mule.routing.outbound.ChainingRouter"> <endpoint address="axis:http://www.webservicex.net/stockquote.asmx?method=GetQuote"> <properties> <property name="soapAction" value="${methodNamespace}${method}"/> <map name="soapMethods"> <list name="qname{GetQuote:http://www.webserviceX.NET/}"> <entry value="symbol;string;in"/> <entry value="GetQuoteResult;string;out"/> </list> </map> </properties> </endpoint> <endpoint address="stream://System.out" transformers="SgmlDecoder Xslt XmlToObject"/> </router> </outbound-router> </mule-descriptor> You can see the full configuration files here - Mule Config Graph for the Stock Quote Exampls
|
![]() |
Document generated by Confluence on Oct 03, 2006 09:23 |