Overview of the ServiceMix 2.x HTTP Binding Example
The following procedure describes how to run the HTTP Binding example in ServiceMix and provides details regarding what the HTTP Binding does. For information on the business use case, please refer to: Use Case for HTTP Binding.
The HTTP Binding example illustrates:
- use of declarative programming
- how to perform an HTTP binding in ServiceMix
The source code for the HTTP Binding example is located in the ServiceMix installation directory under the examples\http-binding directory in the servicemix.xml file. It is recommended that you refer to the source code while reading this document.
In short, this example invokes a web service to ask for a stock price. In more detail, this example shows how to use an HTTP binding to handle a simple HTTP post. One component acts as the HTTP server, which listens on http://localhost:8912, while another component invokes a remote service, implemented as an URLEndpoint. A simple HTTP client is provided so that a simple post can be sent to the server.
Running the HTTP Binding Example
- From a command shell, go to the HTTP Binding example directory:
cd [servicemix_install_dir]\examples\http-binding
where servicemix_install_dir is the directory in which ServiceMix was installed originally.
- Then type:
[servicemix_install_dir]\bin\servicemix servicemix.xml
- To start sending and receiving messages from the HTTP server, send an initial message. To do this, compile and then run a simple HTTP client. The HTTP client used in this example, is built and run from source code using Ant. Execute Ant from the HTTP Binding directory: servicemix_install_dir\examples\http-binding. To run the HTTP client type:
Ant will compile and run the simple HTTP client, HttpClient, which performs a simple post on the HTTP Server into the ServiceMix container, before returning the results to the console.
| Handy Hint
Add $SERVICEMIX_HOME\bin directory to $PATH variable to simplify execution of the example. |
Stopping the HTTP Binding Example
To terminate the HTTP Binding example, type "CTRL-C" in the command shell in which it is running and answer "y" to the "Terminate batch job (y/n)?" question.
How it Works
The diagram below illustrates the flow of messages through the HTTP Binding components:
Messages flow through the components as follows:
- HttpClient, a Java stand-alone program, connects to http://localhost:8912 through the URLConnection class. HttpClient sends the file request.xml to this port.
- httpReceiver, an HTTP server, being a listener on http://localhost:8912 receives the message.
- httpReceiver sends the message to stockQuote as specified in its destinationService property, via NMR.
- stockQuote sends the message into another service, soapEndpoint, for processing.
- soapEndpoint sends the response to stockQuote.
- stockQuote sends the response to httpReceiver via NMR.
- httpReceiver sends the response to http://localhost:8912.
- HttpClient reads the response.
- The response is printed on the console.
Typical output looks like the following two screens.
Output from running [servicemix_install_dir]\bin\servicemix servicemix.xml:
Loading ServiceMix from file: C:\exist\servicemix\servicemix-2.0.2\examples\http
-binding\servicemix.xml
17:34:34.768 EVENT Starting Jetty/4.2.20RC0
17:34:34.848 EVENT Started ServletHttpContext[/]
17:34:34.858 EVENT Started SocketListener on 127.0.0.1:8912
17:34:34.858 EVENT Started org.mortbay.jetty.Server@1f06dc3
Output from running Ant:
Buildfile: build.xml
init:
compile:
run:
[echo] Running example client
[java] <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="htt
p:ema-instance" xmlns:xsd="http:/schemas.xmlsoap.org/soap/encoding/" soap:encodingStyle="http:org/soap/encoding/"><soap:Body><n:getQuoteResponse xmlns:n="urn:xmethods-delayed
-quotes"><Result xsi:type="xsd:float">88.8</Result></n:getQuoteResponse></soap:B
ody></soap:Envelope>asdf
BUILD SUCCESSFUL
Total time: 6 seconds
Details
The following table provides more details about the function of each component and bean in the servicemix.xml file:
Component or Bean ID |
Description |
jbi |
jbi is the "id" of the JBI container and provides the basic infrastructure services for the following components: httpReceiver and stockQuote. During initialization, several singletons are instantiated: URLEndpoint and JBI. After initialization, the components in the JBI container are activated. |
httpReceiver |
This component is an HTTP server that listens at http://localhost/8912. It forwards the message it receives from this URL to stockQuote as specified in its destinationService property in the servicemix.xml file. |
stockQuote |
This is a SaajBinding component that invokes an endpoint service called soapEndpoint. It is implemented by the SaajBinding class which converts an inbound JBI message into a SAAJ (Soap With Attachments for Java) request-response and sends the response back to httpReceiver. This provides a message centric way of invoking SOAP services inside providers such as Apache Axis. This component invokes a web service to get a stock price. |
soapEndpoint |
A URLEndpoint object contains a URL, which is used to make connections to the remote party. A stand-alone client can pass a URLEndpoint object to the SOAPConnection method call to send a message. |
Useful Code Hints
This section describes the start-up sequence and how the ServiceMix container interacts with the HTTP Binding application. The Java class files are located in the servicemix-2.0.2.jar file in the ServiceMix installation directory. To review the Java source code, unjar and decompile the .class files or download the source code.
Viewing the Java source code is recommended for understanding the information in this section.
- The ServiceMix container reads the servicemix.xml file and instantiates the components specified through SpringJBIContainer class.
- The container calls the afterPropertiesSet() method of the SpringJBIContainer to register the components and the activationSpecs. In this case, httpReceiver and stockQuote are registered as activationSpecs.
- The SpringJBIContainer uses the ActivationSpec class as the container for component specific properties, such as routing information.
Some of the ActivationSpec methods are:
- setId - takes the spring:id=jbi and sets the id of the container to "jbi"
- setcomponentName - sets componentName to httpReceiver on first invocation and "stockQuote" in the next
- setEndpoint - sets the endpoint properties from the servicemix.xml file
- setService - sets the service properties from xml file
- setDestinationService - sets the destinationService for each component
HttpConnector Details
- The ServiceMix container reads the servicemix.xml file and to determine that it needs to instantiate a HttpConnector.
- The container calls the init() method of HttpConnector. The init() method sets the listener's host and port property for which the HttpConnector will listen.
- The container calls the start() method of HttpConnector. The start() method will:
A. Add a listener object to the server property of HttpConnector.
B. Create a new HttpContext that contains a ServletHandler class. The ServletHandler maps requests to servlets that implement the javax.servlet.http.HttpServlet API. In this case, the ServletHandler maps the request to the BindingServlet class.
C. The server property of HttpConnector is started.
stockQuote Details
- HttpConnector sends messages to this component as specified on its destinationService property. When this component receives a message, its onMessageExchange() method is called by the container. The onMessageExchange() method will:
A. Create a SOAPConnection to be used in sending the message to URLEndpoint.
B. Marshall an inbound JBI inbound into a SOAPMessage, before sending it to the URLEndpoint, as specified in the soapEndpoint property of stockQuote. A response is also requested from the URLEndpoint.
C. The response, which is a SOAPMessage, is marshalled into a Normalized Message and is eventually sent to HttpConnector.
Related Documentation
For more information on the following topics please see:
For more details on the web service which supplies the stock prices, please see: XMethods.
For a brief explanation of the XML tags in the servicemi.xml file, please see: