DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Explore Salesforce OAuth Authorization Flows and Its Use Cases
  • Build a Flow Collectibles Portal Using Cadence (Part 2)
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)

Trending

  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  • Traditional Testing and RAGAS: A Hybrid Strategy for Evaluating AI Chatbots
  • SaaS in an Enterprise - An Implementation Roadmap
  • Go 1.24+ Native FIPS Support for Easier Compliance

ActiveMQ Read and Write Flow in Mule

Learn how to quickly and easily integrate the ActiveMQ open source messaging provider with a flow in MuleSoft to read and write messages to flows.

By 
Ankit Lawaniya user avatar
Ankit Lawaniya
·
Jul. 28, 17 · Tutorial
Likes (13)
Comment
Save
Tweet
Share
17.9K Views

Join the DZone community and get the full member experience.

Join For Free

Apache ActiveMQ is a popular open source messaging provider, which is easy to integrate with Mule. MuleSoft provides out of the box support for ActiveMQ.

In this tutorial, we will see a basic example of ActiveMQ integration with Mule, where we will be reading and writing messages from/to the queue.

How It Works

To write messages to ActiveMQ, we need a flow that has a JMS outbound endpoint at the end of the flow that writes the message to the queue.

To read messages from ActiveMQ, we need a flow that has a JMS inbound endpoint at the beginning of the flow that reads your queue.

Use Case

We basically have two flows:

1. Write Flow: an HTTP Inbound endpoint receives the Employee XML data through the HTTP Request and forwards it to the JMS queue.

2. Read Flow: reads the staged XMLs from the JMS queue and processes the messages based on the department of Employees.

Here’s an outline of the simple steps required to implement this example:

  • Create an Anypoint Studio project.
  • Import ActiveMQ libraries.
  • Create an ActiveMQ connector.
  • Enqueue the payload to JMS Queue.
  • Dequeue the payload from the JMS Queue.
  • Run the sample.

ActiveMQ Connector Configuration:

<jms:activemq-connector name="Active_MQ2" username="admin" password="password" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>

Image title

JMS Inbound Endpoint Configuration :

<jms:inbound-endpoint queue="myqueue" connector-ref="Active_MQ" doc:name="JMS">
<jms:transaction action="ALWAYS_BEGIN"/>
</jms:inbound-endpoint>


JMS Outbound Endpoint Configuration:

<jms:outbound-endpoint queue="myqueue" connector-ref="Active_MQ" doc:name="JMS"/>


Note: The above mentioned is the minimum configuration that we have used in the demo exercise; there are n number of attributes which can be applied on the inbound and outbound endpoint. To understand all of the attributes, please refer the MuleSoft site.

Mule Flows

Let's walk through how to use an ActiveMQ read and write Mule application.

Write Flow

Create a new flow in Mule Studio and name it “writeFlow.” To configure the project to use ActiveMQ libraries, right-click on the project -> Properties -> Java Build Path -> Add External JARs. Select “activemq-all-5.4.3.jar” from <ACTIVEMQ_HOME>.

Drag and drop the HTTP Listener to the source of the flow to receive the incoming messages. Once the HTTP Listener is in place, drag and drop the JMS Outbound endpoint at the end of the flow. We will use the request payload received from an HTTP inbound endpoint to seed the ActiveMQ Queue.

Image title

Read Flow

Create a new flow in Mule Studio and name it “readFlow.” Drag and drop the JMS Inbound endpoint at the beginning of the flow to receive the incoming messages. Once the message has been received in the flow, then we can start processing the payload.

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.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/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8089"  doc:name="HTTP Listener Configuration"/>
<jms:activemq-connector name="Active_MQ" username="admin" password="password" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="writeFlow">
<http:listener path="/jms" config-ref="HTTP_Listener_Configuration" doc:name="HTTP"/>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<jms:outbound-endpoint queue="myqueue" connector-ref="Active_MQ" doc:name="JMS"/>
</flow>

<flow name="readFlow">
<jms:inbound-endpoint queue="myqueue" connector-ref="Active_MQ" doc:name="JMS">
<jms:transaction action="ALWAYS_BEGIN"/>
</jms:inbound-endpoint>
<set-payload mimeType="application/xml" value="#[payload]" />
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
<choice doc:name="Choice">
<when evaluator="xpath" expression="/Employees/Employee/Department = 'HR'"> 
<logger message="HR process has been started for Employee #[xpath3('//Employees/Employee/Name')] " level="INFO" doc:name="Logger"/>
</when>
<when evaluator="xpath" expression="/Employees/Employee/Department = 'Admin'"> 
<logger message="Admin process has been started for Employee #[xpath3('//Employees/Employee/Name')]" level="INFO" doc:name="Logger"/>
</when>
</choice>               
</flow>
</mule>

Run the Sample

First, run ActiveMQ using the executable “activemq” from <ACTIVEMQ_HOME>/bin folder. Make sure you can access the ActiveMQ admin page using http://localhost:8161/.

Open REST Client and hit the URL with the payload on http://localhost:8089/jms. This will enqueue the request to the ActiveMQ JMS Queue “myqueue.”

Image title

Message Enqueue to JMS Queue

Verify this by examining the ActiveMQ administration page at http://localhost:8161/admin/queues.jsp. We can see that there is one message under the “Messages Enqueued” column.

Image title

Thanks.

Keep learning.

Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Explore Salesforce OAuth Authorization Flows and Its Use Cases
  • Build a Flow Collectibles Portal Using Cadence (Part 2)
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!