Mule 4: JMS Pub and Sub With Transformation
Take a look at a tutorial that explains how to connect to ActiveMQ configuration. Also explore more about the JMS Connector.
Join the DZone community and get the full member experience.
Join For FreeTo know more about JMS pub and sub, go here.
About the JMS Connector
The JMS connector enables the application to exchange messages using the JMS implementation of our own choice. Its main features include:
- Pub/Sub pattern support on any given destination.
- Listen/Reply pattern support on any given destination.
- Publish-Consume pattern support on any given destination, with fixed or temporary reply Queue.
- Fully compliant with JMS 2.0, 1.1 and 1.0.2 specifications.
Mule 4 Example: Connecting to ActiveMQ Configuration
<jms:config name="JMS_Config" doc:name="JMS Config" doc:id="bda7c128-ed6b-4fab-bdfc-0c63148fb16d" >
<jms:active-mq-connection username="XXXXX" password="XXXXX" >
<jms:factory-configuration brokerUrl="tcp://localhost:61616" />
</jms:active-mq-connection>
</jms:config>
Below listed JMS Operations in Mule — 4:
Use Case
Publishing the source message into JMS-Queue and it will consume by the end system. In my case, I am publishing and consuming JSON message from the queue and then converting into XML format.
Mule Flow
Step 1
Configure the HTTP Listener by giving the hostname, port number, and path along with the specific allowed methods (Optional) at the Advanced tab of HTTP connector.
Step 2
Drag and Drop the Logger component to log the inbound message.
Step 3
Configure publish operation of JMS connector from the Mule palette window. And add connector required libraries with connection details as like below.
And specify the Queue name to publish the message.
Step 4
As like in Step 3, configure the consume operation of JMS connector from the Mule palette window.
And specify consuming the Queue name to read the message.
Step 5
Drag and Drop the Transform Message component from the Mule palette window.
From the above screen, SampleData defines the root tag of the XML message as a result.
Step 6
Drag and drop the Logger component to log the result XML message.
Final Mule Flow
Final Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce" xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="4862f344-02c0-4d4d-89f7-2572698011f2" >
<http:listener-connection host="0.0.0.0" port="8084" />
</http:listener-config>
<jms:config name="JMS_Config" doc:name="JMS Config" doc:id="cbf14d84-19d6-4a3d-a288-dde77d6f2a75" >
<jms:active-mq-connection username="admin" password="admin">
<jms:caching-strategy >
<jms:default-caching />
</jms:caching-strategy>
<jms:factory-configuration brokerUrl="tcp://localhost:61616" />
</jms:active-mq-connection>
<jms:consumer-config >
<jms:consumer-type >
<jms:queue-consumer />
</jms:consumer-type>
</jms:consumer-config>
</jms:config>
<flow name="MQ-PUB_SUB" doc:id="1cc1d138-1ead-4098-86ac-5f9cb6c74c96">
<http:listener doc:name="Listener" doc:id="bbd9a567-6729-407f-b77a-71a33fd8bffb" config-ref="HTTP_Listener_config" path="mq" allowedMethods="POST">
</http:listener>
<logger level="INFO" doc:name="Logger" doc:id="e119c173-d4da-4c98-8823-a3a159286b5b" message="#[message.payload]"/>
<jms:publish doc:name="Publish" doc:id="38c403b2-3b92-4924-86ee-e9efff43a8cb" config-ref="JMS_Config" destination="test"/>
<jms:consume doc:name="Consume" doc:id="cbf84a03-9123-4ff1-ae2e-fd98f265c7cb" config-ref="JMS_Config" destination="test">
</jms:consume>
<ee:transform doc:name="Transform Message" doc:id="0dbd6d88-8254-452e-a308-7516b677449e">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/xml
---
SampleData : payload]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="590d01e3-122d-42db-89b4-3845dd45eb3a" message="XML Data -----> #[message.payload]"/>
</flow>
</mule>
Input and Output
H@ppy Le@rning!
Opinions expressed by DZone contributors are their own.
Comments