## Apache Synapse

## Sample 252: Pure Text, Binary and POX Message Support with JMS

<definitions xmlns="http://ws.apache.org/ns/synapse"> <sequence name="text_proxy"> <log level="full"/> <header name="Action" value="urn:placeOrder"/> <script language="js"> var args = mc.getPayloadXML().toString().split(" "); mc.setPayloadXML( &lt;placeOrder xmlns="http://services.samples"&gt; &lt;order&gt; &lt;price&gt;{args[0]}&lt;/price&gt; &lt;quantity&gt;{args[1]}&lt;/quantity&gt; &lt;symbol&gt;{args[2]}&lt;/symbol&gt; &lt;/order&gt; &lt;/placeOrder&gt;); </script> <property action="set" name="OUT_ONLY" value="true"/> <log level="full"/> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </sequence> <sequence name="mtom_proxy"> <log level="full"/> <property action="set" name="OUT_ONLY" value="true"/> <header name="Action" value="urn:oneWayUploadUsingMTOM"/> <send> <endpoint> <address uri="http://localhost:9000/services/MTOMSwASampleService" optimize="mtom"/> </endpoint> </send> </sequence> <sequence name="pox_proxy"> <property action="set" name="OUT_ONLY" value="true"/> <header name="Action" value="urn:placeOrder"/> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/> </endpoint> </send> </sequence> <sequence name="out"> <send/> </sequence> <proxy name="JMSFileUploadProxy" transports="jms"> <target inSequence="mtom_proxy" outSequence="out"/> <parameter name="transport.jms.ContentType"> <rules> <bytesMessage>application/octet-stream</bytesMessage> </rules> </parameter> <parameter name="Wrapper">{http://synapse.apache.org/userguide/samples/}element</parameter> </proxy> <proxy name="JMSTextProxy" transports="jms"> <target inSequence="text_proxy" outSequence="out"/> <parameter name="transport.jms.ContentType"> <rules> <textMessage>text/plain</textMessage> </rules> </parameter> <parameter name="Wrapper">{http://synapse.apache.org/userguide/samples/}text</parameter> </proxy> <proxy name="JMSPoxProxy" transports="jms"> <target inSequence="pox_proxy" outSequence="out"/> <parameter name="transport.jms.ContentType">application/xml</parameter> </proxy> </definitions>

### Objective

Demonstrate the ability of Synapse to receive and mediate plain text, binary and POX (Plain Old XML) messages over JMS.

### Pre-requisites

- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
- Setup and start a JMS broker (Apache ActiveMQ can be used as the JMS broker for this scenario. Refer JMS setup guide for information on how to run ActiveMQ.)
- Enable the JMS transport receiver of Synapse (Refer JMS setup guide for more details)
- Start Synapse using the configuration numbered 252 (repository/conf/sample/synapse_sample_252.xml)
  Unix/Linux: sh synapse.sh -sample 252
  Windows: synapse.bat -sample 252

### Executing the Client

This configuration creates 3 JMS proxy services named JMSFileUploadProxy, JMSTextProxy and JMSPoxProxy exposed over JMS queues with the same names as the services. The first part of this example demonstrates the pure text message support with JMS, where a user sends a space separated text message over JMS of the form '<price> <qty> <symbol>'. Synapse converts this message into a SOAP message and sends this to the placeOrder operation of the SimpleStockQuoteService. Synapse uses the script mediator to transform the text message into a XML payload using the JavaScript support available to tokenize the string. The proxy service property named 'Wrapper' defines a custom wrapper element QName, to be used when wrapping text/binary content into a SOAP envelope.

Execute JMS client as follows. This will post a pure text JMS message with the content defined (e.g. '12.33 1000 ACP') to the specified JMS destination - dynamicQueues/JMSTextProxy.

ant jmsclient -Djms_type=text -Djms_payload="12.33 1000 ACP" -Djms_dest=dynamicQueues/JMSTextProxy

Following the logs, you will notice that Synapse received the JMS text message and transformed it into a SOAP payload as follows. Notice that the wrapper element '{http://synapse.apache.org/userguide/samples/}text' has been used to wrap the text message content.

[jms-Worker-1] INFO LogMediator To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: ID:orcus.veithen.net-50631-1225235276233-1:0:1:1:1, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <axis2ns1:text xmlns:axis2ns1="http://synapse.apache.org/userguide/samples/">12.33 1000 ACP</axis2ns1:text> </soapenv:Body> </soapenv:Envelope>

Then you can see how the script mediator creates a stock quote request by tokenizing the text as follows.

[jms-Worker-1] INFO LogMediator To: , WSAction: urn:placeOrder, SOAPAction: urn:placeOrder, MessageID: ID:orcus.veithen.net-50631-1225235276233-1:0:1:1:1, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <placeOrder xmlns="http://services.samples"> <order> <price>12.33</price> <quantity>1000</quantity> <symbol>ACP</symbol> </order> </placeOrder> </soapenv:Body> </soapenv:Envelope>

This SOAP message is then sent to the SimpleStockQuoteService on the sample Axis2 server. The sample Axis2 server will accept the one-way message and print the following log:

samples.services.SimpleStockQuoteService :: Accepted order for : 1000 stocks of ACP at $ 12.33

The next section of this example demonstrates how a pure binary JMS message can be received and processed through Synapse. The configuration creates a proxy service named 'JMSFileUploadProxy' that accepts binary messages and wraps them into a custom element '{http://synapse.apache.org/userguide/samples/}element'. The received message is then forwarded to the MTOMSwASampleService using the SOAP action 'urn:oneWayUploadUsingMTOM' while optimizing binary content using MTOM. To execute this sample, use the JMS client to publish a pure binary JMS message containing the file './../../repository/conf/sample/resources/mtom/asf-logo.gif' to the JMS destination 'dynamicQueues/JMSFileUploadProxy' as follows:

ant jmsclient -Djms_type=binary -Djms_dest=dynamicQueues/JMSFileUploadProxy \ -Djms_payload=./../../repository/conf/sample/resources/mtom/asf-logo.gif

Examining the Synapse logs will reveal that the binary content was received over JMS and wrapped with the specified element into a SOAP infoset as follows:

[jms-Worker-1] INFO LogMediator To: , WSAction: urn:mediate, SOAPAction: urn:mediate, MessageID: ID:orcus.veithen.net-50702-1225236039556-1:0:1:1:1, Direction: request, Envelope: <?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <axis2ns1:element xmlns:axis2ns1="http://synapse.apache.org/userguide/samples/">R0lGODlhgw...AAOw==</axis2ns1:element> </soapenv:Body> </soapenv:Envelope>

Thereafter the message is sent as a MTOM optimized message as specified by the 'format=mtom' attribute of the endpoint, to the MTOMSwASampleService using the SOAP action 'urn:oneWayUploadUsingMTOM'. Once received by the sample service, it is saved into a temporary file and could be verified for correctness.

Wrote to file : ./../../work/temp/sampleServer/mtom-4417.gif

The final section of this example shows how a POX JMS message is received by Synapse and sent to the SimpleStockQuoteService as a SOAP message. Use the JMS client as follows to create a POX (Plain Old XML) message with a stock quote request payload (without a SOAP envelope), and send it to the JMS destination 'dynamicQueues/JMSPoxProxy' as follows:

ant jmsclient -Djms_type=pox -Djms_dest=dynamicQueues/JMSPoxProxy -Djms_payload=MSFT

Synapse converts the POX message into a SOAP payload and sends to the SimpleStockQuoteService after setting the SOAP action as 'urn:placeOrder'. The sample Axis2 server displays a successful message on the receipt of the message as:

samples.services.SimpleStockQuoteService :: Accepted order for : 19211 stocks of MSFT at $ 172.39703010684752

Back to Catalog