Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

The following procedure describes how to run the HTTP Binding example in ServiceMix and provides details regarding what it 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.

This example shows how to use the an HTTP bindings binding to handle a simple HTTP post. One component acts as the HTTP server thahttp://docs.codehaus.org/display/SM/Http+Binding+Ja
Viewt , which listens on http://localhost:8912Image Removed, while another component invokes a remote service, implemented as a an URLEndpoint. A simple HTTP client is provided so that a simple post can be set sent to the server.

Running the HTTP Binding Example

  1. From a command shell, go to the HTTP Binding example directory:
    Code Block
    cd [servicemix_install_dir]\examples\http-binding
    
    where servicemix_install_dir is the directory in which ServiceMix was installed originally.
  2. Then type:
    Code Block
    [servicemix_install_dir]\bin\servicemix servicemix.xml
    
  3. To start sending and receiving of 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:
    Code Block
    ant
    

    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.
Tip
titleHandy Hint

Add $SERVICEMIX_HOME/bin directory to $PATH variable to simplify execution of the examples.


Stopping the HTTP Binding Example

...


Messages flow through the components as follows:

  1. HttpClient, a java standalone program Java stand-alone program, connects to http://localhost:8912Image Removed through the URLConnection class. It then HttpClient sends the file "request.xml" to this port.
  2. httpReceiver, an http HTTP server, being a listenser listener on http://localhost:8912Image Removed receives the message.
  3. It then httpReceiver sends the message to stockQuote as specified in its destinationService property, via NMR.
  4. stockQuote sends in the message into another service, soapEndpoint for processing.
  5. soapEndpoint sends the response to stockQuote.
  6. stockQuote send sends the response to httpReceiver via NMR.
  7. httpReceiver sends the response to http://localhost:8912Image Removed.
  8. HttpClient reads the response.
  9. The response is printed on the console.

Typical output looks like the following:

Output of from running [servicemix_install_dir]\bin\servicemix servicemix.xml:

Code Block
  
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 of HttpClient.java from running Ant:

Code Block
Buildfile: build.xml

init:

compile:

run:
     [echo] Running example client
     [java] <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="htt
p://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSch
ema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http:/
/schemas.xmlsoap.org/soap/encoding/" soap:encodingStyle="http://schemas.xmlsoap.
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

...

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 jbiJBI.
After initialization, the components in the jbi JBI container are activated.

httpReceiver

This component is an http HTTP server that is set to listen listens at

http://localhost/8912Image Removed

. It forwards the message it receives from this url URL to stockQuote as specified in its property destinationServicedestinationService 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 outputs sends the response back into to httpReceiver. This provides a message centric way of invoking SOAP services inside providers such as Apache Axis.

soapEndpoint

A URLEndpoint object contains a URL, which is used to make connections to the remote party. A standalone stand-alone client can pass a URLEndpoint object to the SOAPConnection method call to send a message.

...

This section describes the start-up sequence and how the ServiceMix container interacts with the File HTTP Binding application. The Java class files are located in the servicemix-12.0.12.jar file in the ServiceMix installation directory. To look at review the Java source code, unjar and decompile the .class files or download the source code. Please note: the downloadable source code is slightly different than the compiled binary code.

Viewing the Java source code is recommended for understanding the information in this section.

filePoller Details

  1. The ServiceMix container reads the servicemix.xml file and instantiates the components specified through SpringJBIContainer class.
  2. SpringJBIContainer registers The container calls the afterPropertiesSet() method of the SpringJBIContainer to register the components and the activationSpecs in its afterPropertiesSet method. In this case, httpReceiver and stockQuote are registered as activationSpecs.
  3. The container determines that FilePoller is an MBean and, therefore, calls the start() method of FilePoller, which it inherits from its parent PollingComponentSupport.The start() method will: (See: PollingComponentSupport.java code fragment below)
    A. Create a timerTask.SpringJBIContainer uses the ActivationSpec class as the container for component specific properties, such as routing information.

Some of the ActivationSpec methods are:

  1. setId - takes the spring:id=jbi and sets the id of the container to "jbi"
  2. setcomponentName - sets componentName to httpReceiver on first invocation and "stockQuote" in the next
  3. setEndpoint - sets the endpoint properties
    B. Schedule the timerTask at a fixed rate. The start() method uses the "timer" (created in the init method) to schedule the timerTask at a fixed rate: timer.scheduleAtFixedRate(timerTask, firstTime, period). Recall "period" is a property of FilePoller. It was assigned the value of 1000ms by dependency injection from the servicemix.xml file.
    C. timer will call the run() method of the timerTask periodically. timerTask's run method() is defined inline. This run() method will get the workManager object (see below for details).
    D. The workManager will call "scheduleWork(PollingComponentSupport.this)". Note: it passes in PollingComponent support, which by virtue of its inheritance hierarchy, is of type "Work", which is a "Runnable" object.
    E. "workManager.scheduleWork(Work)" will get a Thread, passing in a Runnable object, i.e. PollingComponentSupport, and call its run() method.
    F. PollingComponent's run() method calls poll(), which is implemented in FilePoller.
    G. From this point on the call sequence can be followed in FilePoller...
    H. The start() method of PollingComponentSupport, will eventually call super.start(), which propagates up to call the start() method of BaseLifeCycle, which sets the component state to "RUNNING."

Eventually, one of the threads that is polling (see step G) for a file in the inbox directory will see a one. It will use workManager's thread pool to get a thread for processing the file. Processing the file consists of streaming it from inbox, creating a normalized message, and sending the message to the NMR.

Code Block
titlePollingComponentSupport.java
borderStylesolid

  timerTask = new TimerTask() {
     public void run() {
          try {
                getWorkManager().scheduleWork(PollingComponentSupport.this);
          }
          catch (Throwable e) {
                log.error("Failed to schedule work: " + e, e);
          }
     }
  };
  if (firstTime != null) {
     timer.scheduleAtFixedRate(timerTask, firstTime, period);
  }
  else {
     timer.scheduleAtFixedRate(timerTask, delay, period);
  }
}
super.start();

workManager Details

workManager is a property of the FilePoller object. This property is defined by a local reference, the "ref" attribute in the servicemix.xml file. The local reference is a bean which instantiates org.activemq.work.SpringWorkManager.

The SpringWorkManager is a Spring bean. By default when a Spring bean starts, the properties are set, and then the afterPropertiesSet() method is called by the container.

The workManager is used to allocate threads. The FilePoller asks for threads from the workManager for two operations:

  1. setService - sets the service properties from xml file
  2. setDestinationService - sets the destinationService for each component

HttpConnector Details

  1. The ServiceMix container reads the servicemix.xml file and to determine that it needs to instantiate a HttpConnector.
  2. 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.
  3. 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

  1. 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
  2. The timerTask uses threads from the thread pool to periodically check the inbox directory for files.
  3. The workManager will also allocate a thread to process a file (read, normalize and send to NMR). The workManager calls a scheduleWork() method which is non-blocking. Therefore, if multiple files need to be processed, FilePoller can continue making requests to the workManager to schedule work.

Related Documentation

For more information on the following topics please see:

For a brief explanation of the XML tags on servicemi.xml, please see: