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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • AI: Do You Trust It?
  • Spring Boot Application With Spring REST and Spring Data MongoDB
  • Accelerate Innovation by Shifting Left FinOps: Part 4
  • Building A Simple AI Application in 2023 for Fun and Profit

Trending

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Contextual AI Integration for Agile Product Teams
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  1. DZone
  2. Data Engineering
  3. Data
  4. Sharing Data Across Mule Applications

Sharing Data Across Mule Applications

With this approach, it becomes easy share data between applications at runtime without having a database or a file system shared between them.

By 
Anirban Sen Chowdhary user avatar
Anirban Sen Chowdhary
·
Dec. 22, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
48.0K Views

Join the DZone community and get the full member experience.

Join For Free

We are often required to share data between multiple applications. We find ourselves in a situation where we need data to persist for later retrieval and sharing across multiple applications.

One of the ways is using the option of a database shared across multiple applications. In Mule, the other way (and the easiest way) we can do this is by using the Object Store.

Mule uses the Object Store in various filters, routers, and other message processors that need to store their state between messages. Most of the time, Mule manages the Object Store automatically and no user configuration is required. It’s very easy to use the Object Store in Mule for storing data and sharing, which we will explore now in our demonstration.

In this article, we will simply demonstrate using the Object Store and sharing the data between different Mule applications.

Let’s consider we have created two different applications, app1 and app2, which need to share the data between them. We will keep both the application be under a domain called MyDomain.

The graphical representation is as follows:

Image title

Here, we will be demonstrating the sharing of data between two different Mule applications, app1 and app2, under the same domain called MyDomain.

Let’s start by creating a domain application from the domain project. Name the domain project MyDomain. The domain project MyDomain will have the following configuration:

   <?xml version="1.0" encoding="UTF-8"?>

<mule-domain xmlns="http://www.mulesoft.org/schema/mule/domain"

             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

             xmlns:http="http://www.mulesoft.org/schema/mule/http"

             xmlns:spring="http://www.springframework.org/schema/beans"

             xsi:schemaLocation="http://www.mulesoft.org/schema/mule/domain http://www.mulesoft.org/schema/mule/domain/current/mule-domain.xsd

               http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd

               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd">

    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081"/>

      <spring:beans>

         <spring:bean id="myListableObjectStore"  class="org.mule.util.store.SimpleMemoryObjectStore" />

       </spring:beans>

</mule-domain>     

We can see above that the domain project defines the Object Store and an HTTP listener connector, which will be shared across all the applications under this domain.

The next step will be creating app1 and app2 under MyDomain.

Adding Data From App1

In app1, we will create a flow that will store data into the Object Store. We will store the data in the Object Store by adding a key which will increment by one whenever adding a new data to the Object Store:

  <flow name="storeDataFlow">

     <http:listener config-ref="HTTP_Listener_Configuration1" path="/set" doc:name="HTTP" />

     <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object" />

     <set-variable variableName="objectStoreSize" value="#[app.registry.ObjectStore.allKeys().size()]" doc:name="Set count" />

     <objectstore:store config-ref="ObjectStore" key="#[new java.lang.Integer(flowVars['objectStoreSize']+1)]" value-ref="#[payload]" doc:name="AddToObjectStore" />

     <set-payload value="Payload set in ObjectStore !!!!" doc:name="Set Payload" />

     <object-to-byte-array-transformer doc:name="Object to Byte Array" />

 </flow>

Here, we can see that we are using tag <objectstore:store/> to store data with a key that will increment each time we add data into it.

Retrieving Values in App2

In app2, we will be creating the following flows to retrieve all the values of the data that are inserted into Object Store by app1:

<flow name="retrieveAllValuesFlow">

  <http:listener config-ref="HTTP_Listener_Configuration1" path="/getvalues" doc:name="HTTP" />

  <set-variable variableName="AllValues" value="#[new java.util.ArrayList()]" doc:name="Create all message arraylist"/>

   <foreach collection="#[app.registry.ObjectStore.allKeys()]" doc:name="For Each" counterVariableName="i">

         <objectstore:retrieve config-ref="ObjectStore" key="#[new java.lang.Integer(i)]" doc:name="Retrieve message"/>

         <object-to-string-transformer doc:name="Object to String" />

         <logger message="Values #[new java.lang.Integer(i)] :- #[message.payload]" level="INFO" doc:name="Logger" />

         <expression-component doc:name="Add message to all messages"><![CDATA[flowVars['AllValues'].add(payload)]]>

        </expression-component>

      </foreach>

  <set-payload doc:name="Set Payload" value="All the Values #[flowVars['AllValues']] "/>

   </flow>

Here, with the above flow, we will be getting all the data stored by app1 by iterating all the keys associated with the data.

Time to Test Our Applications

So, all set! Now, we are going to deploy both of the applications under MyDomain and start sharing the data between these two.

Since app1 and app2 are under the same domain, if we deploy MyDomain, both applications will automatically get deployed under the Mule server:

Image title

To start with, let’s add some data to the Object Store from app1 by posting the payload to the API.

Here, we are sending the JSON payload with id=01 and name=Anirban:

Image title

Let’s add some more data from app1. Again, we will hit the service and send the JSON payload with id=003 and name=Mike:

Image title

We will repeat the process one more time, hit the service, and send the JSON payload with id=027 and name=David. Now, we have added some data to our Object Store from app1.
Image title

Once done, we have successfully inserted data into the Object Store.

Next, from app2, we will retrieve the entire data value with the help of the keys associated with each data from the Object Store of all the data that we inserted into it from app1.

We have used the tag <objectstore:all-keys/> in app2 to retrieve all the keys of the data inserted from the app1 application.

When we hit http://localhost:8085/getvalues, all the data values associated with the keys will display in the response. Here, we can see that there will be three values inserted into the Object Store:

Image title

Conclusion

As we can see with the above example, it’s very easy to share data across any number of applications in Mule. Here, the data from app1 is accessed by app2. We can extend and modify these applications to do read and write operations on data and vice versa.

We can also perform other operations on the data such as writing the data from one application and deleting the data from other, retrieving all the keys associated with the data in the Object Store, removing a particular key associated with a data in the Object Store by an application, etc. 

With this approach, it becomes easy for us to share our data between applications at runtime without having a database or a file system shared between them.

Data (computing) application

Opinions expressed by DZone contributors are their own.

Related

  • AI: Do You Trust It?
  • Spring Boot Application With Spring REST and Spring Data MongoDB
  • Accelerate Innovation by Shifting Left FinOps: Part 4
  • Building A Simple AI Application in 2023 for Fun and Profit

Partner Resources

×

Comments

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: