Dealing with store – “The Object Store”

Facebooktwittergoogle_plusredditpinterestlinkedinmail

We often get into the situation where some data is require to persist for later retrieval and share across multiple application. Though we can easily use database for this purpose, but we can also use another facility called Object Store in Mule.

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

Let’s consider we have 2 different application app1 and app2 which need to share data between them. Let both the application be under a domain called MyDomain.

Untitled

So, let’s start with our demonstration by creating a Mule Domain project called MyDomain:-

Untitled

 

In our Domain project MyDomain we will be defining Object Store spring bean mule-domain-config.xml as follows:-

Untitled

Now we will create two Mule application app1 and app2 to use the domain and share data between them :-

Untitled

We will deploy our Domain MyDomain along with two application app1 and app2 in Mule server:-

Untitled

 

Adding data to Object Store from app1.

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

Untitled

We are using the tag <objectstore:store/> to store data.

Let’s add few data to Object Store from app1 by posting payload to the API.
Here we are sending JSON payload with id=01 and name=Anirban.

Untitled

 

Again we will hit the service and send JSON payload with id=002 and name=Mike.

Untitled

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

Untitled

Retrieving all the keys from app2

Now, from app2, we will retrieve all the key from the Object Store of all the data that we inserted into it from app1.
We will use tag <objectstore:all-keys/> to retrieve all the Keys.

Untitled

Whenever we will hit http://localhost:8085/getallkeys all the keys associated with the data. Here there will be 3 keys :- 1,2 and 3 for all 3 data we have inserted into Object Store.

Untitled

Retrieving all the values from app2

Here we will retrieve all the values we inserted into Object Store from app1 along with their key from app2.
We will use <objectstore:retrieve/> to achieve this within a for loop.

Untitled

When we hit the url http://localhost:8085/getallvalues we get all the values along with the keys associated:-

Untitled

Deleting values from app2

To delete all the values in Object Store from app2, we can construct the following flow in app2.
We will use <objectstore:remove/> tag inside for loop to remove all values :-

Untitled

 

When we hit the url http://localhost:8085/removeallvalues all the data will be removed from the Object Store:-

Untitled

 

Conclusion…..

Here it was a small demonstration on using Object Store across multiple application to share data between them.Mule uses Object Stores whenever it needs data to persist for later retrieval.

I have shared the full article here :- http://www.slideshare.net/anir37/dealing-with-store-the-objectstore .

So at the end I can only say that, let’s spread our knowledge and expand our Mule community. :)

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Anirban Sen Chowdhary

Anirban Sen Chowdhary is an information technology professional currently working on Java/J2ee, Esb and Integration platform. For more information, please visit http://anirbansenchowdhary.com and you can also follow https://twitter.com/Anir37

You may also like...

1 Response

  1. RamaKrishna Kesara says:

    Thank you very much for sharing very valuable information on mule.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>