Stock Quote Example
This example demonstrates how to invoke a Web Service from Mule ESB, transform the result using XSLT, and deserialize the result to a StockQuote Java bean. The application demonstrates the use of REST, SOAP and WSDL to invoke the service. The process is as follows:
The application uses an HTTP Inbound Endpoint to receive a query from a user
It extracts the quote symbol from the query and invokes the Stock Quote Service with the symbol
Finally, it sends the quote back to the browser, transforming it back into plain text.
This document describes the details of the example within the context of Mule Studio, Mule ESB’s graphical user interface (GUI). Where appropriate, the XML configuration follows the Studio interface screenshot in an expandable section.
As with this Stock Quote example, you can create template applications straight out of the box in Mule Studio or Mule Standalone (Mule ESB without Studio). You can tweak the configurations of these use case-based templates to create your own customized applications in Mule.
Follow the procedure below to create, then run the Stock Quote application in Mule ESB.
Complete the procedure in Examples and Exercises to create, then run the Stock Quote template in Mule Studio, or the Stock Quote example in Mule Standalone (Mule ESB without Studio).
Open a Web browser.
Depending on which flavor of the example you want to use, type one of the following URLs into the address bar of your browser, then press enter.
The last four characters in the URL determine the method the application will use to query the Stock Quote Service. The
symbolparameter allows you to choose the stock symbol whose quote you wish to obtain (in the example above,
Press enter to elicit a response from the application to display data similar to the following:
Some configuration elements for this application are stored in external files. To load the configuration from these files, the application uses Global Elements. To view the Global Elements for the application, click the Global Elements tab under the Studio canvas. The image below highlights the Global Elements tab, and displays the Global Elements configured in this example.
The first Global Element is a Property Placeholder. It loads the properties contained in a configuration file into the Mule context. To see the configuration for this Global Element, double-click Properties Placeholder in the Global Elements tab.
1 <context:property-placeholder location="proxy.properties"/>
In this case, the Property Placeholder reads the file
stockquote.properties (see below).
The next five global elements define the configuration for the transformers. Note that the XSLT transformer references the XSLT file in the application’s
xsl subdirectory. The XSLT file can be anywhere on your classpath.
The HTTP_input flow is the main entry point for queries to this example application. It receives HTTP requests sent to the application’s URL, and determines which subflow to call for processing the request.
The first building block in the flow, an HTTP Endpoint, receives the HTTP request from the application’s URL. The second building block, a Body to Parameter Map transformer, parses the HTTP request into a map. The flow then uses a Flow Processing Strategy to extract the method from the map (REST, SOAP or WSDL), then call the appropriate subflow for processing.
The REST subflow uses a Set Payload transformer to extract the value of the stock symbol from the payload. (This value is defined in the URL; in this example the value is
IBM.) Subsequently the flow sets the
Content-Type property for the payload, which it posts as a form to the REST service using the HTTP Outbound Endpoint. Finally, it decodes the response and applies the XSLT transformation using the
This subflow is very similar to the REST subflow except for its service configuration. Instead of using REST, it employs a SOAP Component in the soapClient subflow to invoke the Stock Quote Service with the extracted symbol as parameter.
The second building block in the flow, called Soap Client, is a Flow Reference Component. It invokes the soapClient subflow, which in turn uses an HTTP Endpoint to post the extracted symbol as a form to the Stock Quote Service. The soapClient flow waits for a reply, then forwards it back to the SOAP subflow. The SOAP subflow decodes the reply, then applies the XSLT transformation using the
Like the REST and SOAP subflows, the WSDL Subflow uses the Set Payload transformer to extract the value of the stock symbol from the payload. It then uses a Generic Outbound Endpoint to post the request form to the Stock Quote Service.
The Outbound Endpoint in the WSDL subflow uses the
wsdl-cxf prefix in its configured address. The limitation of this approach is that it only supports handling primitive data types. The correct approach in a real-life scenario is to use
simple-client as the address prefix.