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.
So, let’s start with our demonstration by creating a Mule Domain project called MyDomain:-
In our Domain project MyDomain we will be defining Object Store spring bean mule-domain-config.xml as follows:-
Now we will create two Mule application app1 and app2 to use the domain and share data between them :-
We will deploy our Domain MyDomain along with two application app1 and app2 in Mule server:-
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 :-
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.
Again we will hit the service and send JSON payload with id=002 and name=Mike.
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.
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.
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.
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.
When we hit the url http://localhost:8085/getallvalues we get all the values along with the keys associated:-
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 :-
When we hit the url http://localhost:8085/removeallvalues all the data will be removed from the Object Store:-
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.