You are viewing an older version of this section. Click here to navigate to the latest version.

Stock Quote Example

[ Running the Application ] [ How it Works ] [ The Web Service Version ]

This example demonstrates how to invoke an ASPX web service from Mule ESB, transform the result using XSLT, and deserialize the result to a StockQuote Java bean. The example demonstrates using REST and Web Services to invoke the service.

The configuration uses a HTTP endpoint to receive a stock symbol from a user, invokes the StockQuote service, transforms the result using the XSLT transformer, and then uses the XmlToObject transformer to convert the result into a StockQuote Java bean. The quote is then sent back to browser, transforming it once more into plain text.

This page describes how to run the Stock Quote example and walks you through its configuration.

Running the Application

  1. Make sure you have met the prerequisites and installed Mule according to the [MULE3INTRO:Installing Mule] instructions.

  2. At the command line, navigate to the examples/stockquote directory under your Mule home directory.

  3. Copy the pre-built application archive to the application folder ($MULE_HOME/apps)

  4. Start Mule

  5. Now invoke one of the following URLs, depending on which example flavor you want to run:

  6. The stock information is displayed.

  7. To stop Mule, type 'CTRL-C' in the Mule console window.

How it Works

This section walks through the configuration of the REST version of the Stock Quote example, but the Web Service configuration is very similar. Assuming you’ve already seen the [MULETEMP:Echo Example], this section will discuss the configuration without going into step-by-step detail.

First, because the proxy settings are in an external properties file, we specify the file in the Mule context:

<context:property-placeholder location=""/>

Next, we configure the HTTP connector with the properties whose values are defined in the file:

  <http:connector name="HttpConnector"                  proxyHostname="${proxyHostname}"                  proxyPassword="${proxyPassword}"                  proxyPort="${proxyPort}"                  proxyUsername="${proxyUsername}"/>

The next section is the configuration for the transformers. There are four transformers, which will be chained together. Note that the XSLT transformer references the XSLT file in the xsl subdirectory under the stockquote directory. The XSLT file can be anywhere on your classpath.

  <xm:xml-to-object-transformer name="XmlToObject"/>  <xml-entity-decoder-transformer name="XmlDecoder"/>  <xm:xslt-transformer name="Xslt" xsl-file="xsl/rest-stock.xsl"/>  <object-to-string-transformer name="ToString"/>

Next, we set up a flow as the main entry point into this example. This flow receives requests sent to the HTTP URL and transforms the request’s body to a Map using the HttpRequestToMap transformer. It then extracts the method from the Map to determine which sub-flow to call for processing.

<flow name="HTTP input">    <http:inbound-endpoint host="localhost" port="48309" path="stockquote">        <http:body-to-parameter-map-transformer />        <response>            <object-to-string-transformer />        </response>    </http:inbound-endpoint>    <choice>        <when expression="payload.method == 'REST'" evaluator="groovy">            <flow-ref name="REST" />        </when>        <when expression="payload.method == 'SOAP'" evaluator="groovy">            <flow-ref name="SOAP" />        </when>        <when expression="payload.method == 'WSDL'" evaluator="groovy">            <flow-ref name="WSDL" />        </when>    </choice>    <transformer ref="XmlToObject" /></flow>

The component is the REST service component, which uses the REST service wrapper to proxy a REST service to act like a local Mule component. The REST service wrapper 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 associate with the message payload. In this case, we have only one parameter name, "symbol". The httpMethod can either be GET or POST.

<flow name="REST">    <transformer ref="ExtractSymbol" />    <http:rest-service-component serviceUrl=""        httpMethod="POST">        <http:payloadParameterName value="symbol" />    </http:rest-service-component>    <transformer ref="XmlDecoder" />    <transformer ref="XsltRest" /></flow>

The Web Service Version

The Web Service version works very similarly to the REST version but has different service configuration. The Web Service version explicitly configures an outbound pass-through router that that takes the input from one endpoint and passes it directly to the outbound CXF endpoint. The outbound endpoint is configured with parameters that map to the Stock Quote service.

<flow name="SOAP">    <transformer ref="ExtractSymbol" />    <outbound-endpoint address="">        <cxf:jaxws-client clientClass="net.webservicex.StockQuote" operation="GetQuote"            port="StockQuoteSoap" wsdlLocation="classpath:stockquote.wsdl" />    </outbound-endpoint>    <transformer ref="XmlDecoder" />    <transformer ref="XsltSoap" /></flow>