Versions Compared

Key

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

Overview of the ServiceMix 2.x

...

HTTP Binding Example

The following procedure describes how to run the Http 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 JMS HTTP Binding.

The JMS HTTP Binding example illustrates:

  • use of declarative programming
  • how to perform Http an HTTP binding in ServiceMix

The source code for the Http 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 connect the Http bindings to a simple service componentuse an HTTP binding to handle a simple HTTP post. One component listens for a JMS message on topic 'demo.org.servicemix.source' while another publishes processed messages to topic 'demo.org.servicemix.result'. A simple JMS 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 messages a simple post can be sent and received to and from the ServiceMix Containerserver.

Running the

...

HTTP Binding Example

  1. From a command shell, go to the JMS 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 HTTP server, send an initial message. To do this, compile and then run a simple Http HTTP client. The HTTP client used in this example, is built and run from source code using Ant. Execute Ant from the JMS HTTP Binding directory: servicemix_install_dir\examples\http-binding. To run the Http HTTP client type:
    Code Block
    ant
    

    Ant will compile and run the simple Http HTTP client, HttpClient, which performs a simple post on the Http 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

To terminate the Http 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.

...

The diagram below illustrates the flow of messages through the Http HTTP Binding components:

Panel
borderColor#ccc
titleHttp HTTP Binding Example Message Flow Diagram
borderStylesolid


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 from running [servicemix_install_dir]\bin\servicemix servicemix.xml:

Code Block

  
Loading ServiceMix from file: 
Code Block

  
Loading ServiceMix from file: C:\exist\servicemix\servicemix-2.0.1\examples\jms--binding\servicemix.xml
[INFO] XBeanXmlBeanDefinitionReader - Loading XML bean definitions from file [C:\exist\servicemix\servicemix-2.0.12\examples\jmshttp
-binding\servicemix.xml]
[INFO] FileSystemXmlApplicationContext - Bean factory for application context [org.xbean.spring.context.FileSystemXmlApplicationContext;hashCode=22540508]: 
 org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans[jbi,jencks,broker,
transactionContextManager,transactionManager,jmsFactory]; root of BeanFactory hierarchy
[INFO] FileSystemXmlApplicationContext - 6 beans defined in application context [org.xbean.spring.context.FileSystemXmlApplicationContext;hashCode=22540508]
[INFO] CollectionFactory - JDK 1.4+ collections available
[INFO] CollectionFactory - Commons Collections 3.x available
[INFO] FileSystemXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.Delegating
MessageSource@a8c488]
[INFO] FileSystemXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': 
using default [org.springframework.context.event.SimpleApplicationEventMulticaster@76cbf7]
[INFO] DefaultListableBeanFactory - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans
 [jbi,jencks,broker,transactionContextManager,transactionManager,jmsFactory]; root of BeanFactory hierarchy]
[INFO] SpringBrokerContainerFactory - Loading ActiveMQ broker from configuration: class path resource [activemq.xml]
[INFO] ActiveMQBeanDefinitionReader - Loading XML bean definitions from class path resource [activemq.xml]
[INFO] BrokerContainerImpl - ActiveMQ 3.2 JMS Message Broker (ID:el2tong-1857-1132035585799-1:0) is starting
[INFO] BrokerContainerImpl - For help or more information please see: http://www.logicblaze.com
[INFO] JDBCPersistenceAdapter - Database driver recognized: [apache_derby_embedded_jdbc_driver]
[INFO] DefaultJDBCAdapter - Could not create JDBC tables; they could already exist. Failure was: 
CREATE TABLE ACTIVEMQ_MSGS(ID INTEGER NOT NULL, CONTAINER VARCHAR(250), MSGID VARCHAR(250), MSG BLOB, PRIMARY KEY ( ID ) ) Message: Table/View
'ACTIVEMQ_MSGS' already exists in Schema 'APP'. SQLState: X0Y32 Vendor code: 20000
[INFO] DefaultJDBCAdapter - Could not create JDBC tables; they could already exist. Failure was:
 CREATE TABLE ACTIVEMQ_TXS(XID VARCHAR(250) NOT NULL, PRIMARY KE
Y ( XID )) Message: Table/View 'ACTIVEMQ_TXS' already exists in Schema 'APP'. SQLState: X0Y32 Vendor code: 20000
[INFO] DefaultJDBCAdapter - Could not create JDBC tables; they could already exist. Failure was: CREATE TABLE ACTIVEMQ_ACKS(SUB VARCHAR(250) NOT NULL, CONTAINER
 VARCHAR(250) NOT NULL, LAST_ACKED_ID INTEGER, SE_ID INTEGER, SE_CLIENT_ID VARCHAR(250), SE_CONSUMER_NAME VARCHAR(250), SE_SELECTOR VARCHAR(250), PRIMARY KEY (
SUB, CONTAINER )) Message: Table/View 'ACTIVEMQ_ACKS' already exists in Schema 'APP'. SQLState: X0Y32 Vendor code: 20000
[INFO] DefaultJDBCAdapter - Could not create JDBC tables; they could already exist. Failure was: ALTER TABLE ACTIVEMQ_MSGS ADD EXPIRATION BIGINT Message: Column
 'EXPIRATION' already exists in Table/View 'APP.ACTIVEMQ_MSGS'. SQLState: X0Y32 Vendor code: 20000
[INFO] DefaultJDBCAdapter - Could not create JDBC tables; they could already exist. Failure was: ALTER TABLE ACTIVEMQ_MSGS ADD SENT_TO_DEADLETTER CHAR(1) 
Message: Column 'SENT_TO_DEADLETTER' already exists in Table/View 'APP.ACTIVEMQ_MSGS'. SQLState: X0Y32 Vendor code: 20000
[INFO] JournalPersistenceAdapter - Opening journal.
[INFO] JournalPersistenceAdapter - Opened journal: Active Journal: using 2 x 20.0 Megs at: ..\var\journal
[INFO] JournalPersistenceAdapter - Journal Recovery Started.
[INFO] JournalPersistenceAdapter - Journal Recovered: 0 message(s) in transactions recovered.
[INFO] TcpTransportServerChannel - Listening for connections at: tcp://el2tong:61616
[INFO] BrokerConnectorImpl - ActiveMQ connector started: TcpTransportServerChannel@tcp://el2tong:61616
[INFO] BrokerContainerImpl - ActiveMQ JMS Message Broker (ID:el2tong-1857-1132035585799-1:0) has started
[INFO] JCAContainer - Jencks JCA Container (http://jencks.org/) has started running version: 1.1
[INFO] SpringInitialContextFactory - Loading JNDI context from: class path resource [jndi.xml]
[INFO] XBeanXmlBeanDefinitionReader - Loading XML bean definitions from class path resource [jndi.xml]
[INFO] JBIContainer - Activating component for: [container=defaultJBI,name=#SubscriptionManager#,id=#SubscriptionManager#]
 with service: null component: org.servicemix.jbi.nmr.SubscriptionManager@1315415
[INFO] JBIContainer - ServiceMix JBI Container (http://servicemix.org/) name: defaultJBI running version: 2.0.1
[INFO] JBIContainer - Activating component for: [container=defaultJBI,name=inputReceiver,id=inputReceiver]
 with service: {http://servicemix.org/demo/}inputReceiver component: org.servicemix.components.jms.JmsInUsingJCABinding@1786a3c
[INFO] JCAConnector - Activating endpoint for activationSpec: ActiveMQActivationSpec{acknowledgeMode='Auto-acknowledge', destinationType='javax.jms.Topic', mess
ageSelector='null', destination='demo.org.servicemix.source', clientId='null', subscriptionName='null', subscriptionDurability='NonDurable'} using endpointFacto
ry: org.jencks.SingletonEndpointFactory@d0d12e[transactionManager=org.apache.geronimo.transaction.context.GeronimoTransactionManager@1554233]
[INFO] ComponentContextImpl - Component: inputReceiver activated endpoint: {http://servicemix.org/demo/}inputReceiver : inputReceiver
[INFO] JBIContainer - Activating component for: [container=defaultJBI,name=outputSender,id=outputSender] with service:
 {http://servicemix.org/demo/}outputSender component: org.servicemix.components.jms.JmsSenderComponent@1c6d11a
[INFO] ComponentContextImpl - Component: outputSender activated endpoint: {http://servicemix.org/demo/}outputSender : outputSender
[INFO] ActiveMQConnection - channel status changed: Channel: TcpTransportChannel: Socket[addr=localhost/127.0.0.1,port=61616,localport=1863] has connected
[INFO] BrokerContainerImpl - Adding new client: ID:el2tong-1857-1132035585799-11:0 on transport: TcpTransportChannel:
 Socket[addr=/127.0.0.1,port=1863,localport=61616]


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:

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

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: inputReceiver httpReceiver and outputSenderstockQuote. During initialization, several singletons are instantiated: transactionManager, broker, jencks, jmsFactory, and jbiURLEndpoint and JBI. After initialization, the components in the jbi JBI container are activated.inputReceiver

httpReceiver

This component uses jencks , the JCA container, to listen on port 61616 for a JMS message on the topic called "demo.org.servicemix.source." inputReceiver subscribes to the demo.org.servicemix.source topic. It takes the message, normalizes it, and routes it to outputSender via the NMR. This component combined with outputSender and supporting beans creates a JMS bridge between two topics.

outputSender

This component receives a normalized message from the NMR, marshals it into a JMS message using jmsTemplate (1), then publishes it on the ActiveMQ (JMS) topic called "demo.org.servicemix.result."

jencks

The jencks bean defines a JCA container. The JCA container allows you to configure thread pool size, configure a transaction manager, and configure a resource adapter. In this example, the resource adapter is an ActiveMQ adapter, however, any JMS listener could be configured instead. This adapter listens on port 61616 for JMS messages.

broker

The broker bean uses the activemq.xml file to configure the message broker, which handles the JMS messages for the components that require JMS messaging services.

transactionManager

This bean is configured to be the default transaction manager for the jbi container. jencks is configured to use the default transaction manager. This transaction manager provides transactional services between the resource adapter (in this case the ActiveMQ resource adapter provided by the jencks JCA container) and components the jbi container.

jmsFactory

This bean listens on port 61616 and provides a pooled ActiveMQ connection.

...

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.

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.

  1. The ServiceMix container reads the servicemix.xml file and instantiates the components specified through SpringJBIContainer class.
  2. 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.
  3. The 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 from the servicemix.xml file
  4. setService - sets the service properties from xml file
  5. 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.

Related Documentation

For more information on the following topics please see:

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