MuleSoft—Let’s take a “Mysterious” ride with Object Store V2

Share Object Store V2 between mule applications

Amlan Mahapatra
The Mule Blog
10 min readJun 8, 2020

--

Often in our projects we need to create or manipulate an object store in one application. But, in this tutorial we will learn “how to access or modify an object store V2 from different application”. Yes, you read it correctly — “from different application”.

The discussion is based on Object Store V2 implementation on Mule runtimes 4.x

So let’s start with the basic, what is object store?

Basically, Object stores are used to store an object . In Mule, we uses object stores whenever it needs data persistent for later retrieval and by the Object store we can serialize data in key/value format.

Now, let’s discuss about the object store connectors’s operations?

The object store connector can perform various operations like Clear, Contains, Remove, Retrieve, Retrieve all keys, Retrieve all and Store.

Clear — Removes all the contents in the store.

Contains — Checks if there is any value associated to the given key. If no value exists for the key, then false is returned.

Remove — Removes the value associated to the given key. If no value exists for the key, then an OS:KEY_NOT_FOUND error is thrown.

Retrieve — Retrieves the value stored for the given key. If no value exists for the key, behavior depends on the defaultValue parameter.

Retrieve all keys — Returns a List containing all keys that the object store currently holds values for.

Retrieve all — Retrieves all the key value pairs in the object store.

Store — Stores the given value using the given key. This operation can be used either for storing new values or updating existing ones, depending on the value of the failIfPresent.

OK, now we have got the basic idea about object store V2.

Now let’s check some interesting FAQ of object store V2 as per the mule documentation.

Can one Mule app access the object store of another Mule app?

No. You can use Object Store V2 only as a distributed memory store for an application or as a key-value cache. It is not designed for app-to-app communication.

But, wait a second if I say yes, you can fetch or modify object stores from different application.

We are The Developers. In our career, we have faced some problems which has forced us to think differently.

In one of my recent project I solved this problem. The problem was like “ One application is used only for storing object store V2, and another application is used to fetch or modify these object stores V2 values ”.

OK, now let’s solve the problem . Stay with me to enjoy the ride. Let’s get started.

In this tutorial , we will create two applications. First is “objectstore-called” which will be responsible for storing the object. And second is “objectstore-calling” which will be responsible for fetching the object stores values from the “objectstore-called” application and modify(Update/Remove) it as per your requirements.

To make this tutorial simple , we will use only three connectors or operations of object store which are “Contains”, “Store” and “Retrieve”.

Step 1 : First create the application for storing the object store means in this example that is “objectstore-called”.

We all know how to create a new projects in Anypoint studio, so I skip the project creation part.

First we will create a flow with the help of object store’s Store and Contains connector which will help to store or update the values of the object store.

Set the /Store as Path and change the Allowed methods to POST (Click on Advance ==> Allowed methods). Http Connector configuration will be the default one means 8081.

In this flow , we will create or update a key, if the key is available then it will update the value else it will create. To perform this checking we will use object store’s contains connector. As we all know, object store’s contains connector returns boolean value. It will return true if the key is available, else it will return false.

In this Contains connector configuration, we just put “attributes.queryParams.osKey” as key because we are sending key as queryPamar named osKey and change the Target Variable of the object store’s contains connector to “check1" , otherwise this value will overwrite the payload. Like-

Now put a Choice connector to check the value of variable “check1", if the value is false then it will be create else update.

We will put object store’s Store connector in both cases, whether it is create or update. In the Store connector, we will use two value “attributes.queryParams.osKey” as key and “attributes.queryParams.osValue” as value. And set a required payload for both , but its not mandatory.

The required request structure for this flow (POST)-

OK, our Create/Update flow is now ready.

Now we will create the flow for fetching the object store value.

Set the /Fetch as Path and change the Allowed methods to GET(Click on Advance ==> Allowed methods). Http Connector configuration will be the default one means 8081.

In the same way , first we will check the key whether it is available on not with the help of Contains connector else the flow will give an error “OS:KEY_NOT_FOUND” when you search for a key which doesn't exist.

Only you have to change the target variable of the Contains connector to “check2”. I already explain you how to change the target variable. And pass the same “attributes.queryParams.osKey” as key in the Contains Connector configuration like the first one. So I skip this part.

Now drag a Choice connector and in the When part put the condition “vars.check2 == true” and drag a Retrieve connector in it . In the Default part of the choice, just put a payload and set a static value like “key not found”. Because we all know that if the Contains connector returns “true” that means key is available.

The required request structure for this flow (GET)-

OK, our flow for fetching object store value is ready.

Now the last one, means the flow for remove object store.

Set the /Remove as Path and change the Allowed methods to DELETE(Click on Advance ==> Allowed methods). Http Connector configuration will be the default one means 8081.

In the same way , first we will check the key whether it is available on not with the help of Contains connector else the flow will give an error “OS:KEY_NOT_FOUND” when you search for a key which doesn’t exist. Only change is the target value of the Contains connector that is “check3”.

Again we check for key whether it is available or not with the help of Choice block. And if the key is available means if the Contains connector comes with true then just remove it. Here we will use the Remove connector. In this connector, we will pass “attributes.queryParams.osKey” as key and then add a payload as per your requirement but this payload is not mandatory. In the Default part of the choice, just put a payload and set a static value like “key not found”.

The required request structure for this flow (DELETE)-

OK, now our first application is ready.

Now we will deploy it to CloudHub. After deploying, you will find your “AppUrl”.

OK, now you can test the application by using the Postman Rest Client.

Step 2: Now we will create the application which will fetch or modify the values of object stores of the previous application.

So, create a new Mule Application named “objectstore-calling”

We will first create a flow which will create or update the object store of the first application (objectstore-called). If the object is available in the first application (objectstore-called) then this flow will update the value else it will create a new one.

We will use a http listener and a http request connector to make this flow.

Set /CreateUpdate as Path of http connector and rest configurations of http listener remain the default one.

Now we will hit the /Store url of the first application (objectstore-called) which is responsible for object store’s create/update operation.

So /Store url is like “{AppUrl}/Store?osKey=Sova1&osValue=Sova Biswas”. We have to configure it in a http request connector in the second application(objectstore-calling).

So first we will create the configuration of the http request connector by clicking in the “+”.

Now just put the CloudHub App Url of the first application (objectstore-called) in the host and rest remain the same.

OK, now our global configuration of http request is done. We will reuse it later also.

Now set the other configuration of http request connector like-

Select POST as method, /Store as Path and set the two parameter payload.key as osKey and payload.value as osValue.

The required request structure for this flow-

Now if you hit this url again with the same key but with different value, the it will update the value of this key.

Now we will go to the first application (objectstore-called) which is deployed in cloudhub to check the object store.

So we are updating or creating an object store from different application. Now, I think you understand the trick.

OK, the create/update flow is ready.

Now we will create a flow which will fetch the object store value form the first application(objectstore-called) in the similar fashion.

We will use the same http listener configuration only we have to set the path as /Access.

To fetch the value of object store we have to hit the /Fetch endpoint of the first application. The url is like - “{AppUrl}/Fetch?osKey=Sova1

Since the AppUrl is same , so we can reuse the http request connectors’s global configuration. Only the Method, Path and the queryParam will be different.

The required request structure for this flow-

Now if want to search for a key which is not available in first applicatio (objectstore-called)-

OK, fetching object store value flow is also ready.

Now, we will create our last flow which will delete the object store.

We will use the same http listener global configuration, only will change the path.

Now to remove the object store, we have to hit the /Remove endpoint of the first application. The url is “{AppUrl}/Remove?osKey=Sova1”.

Let’s configure the http request connector for this url. Again we will use the same http request global configuration, only we have to change the the Method, Path and queryParam.

The required request structure for this flow-

Now if you go to cloudhub to check the object store, you will see that object store is deleted.

Now if want to delete a object store which is not available in first applicatio (objectstore-called)-

That’s it.

Hope you enjoyed the ride and this tutorial helps you understand object store and how to access object store from different application, will come back later with more stuffs.

References

--

--

Amlan Mahapatra
The Mule Blog

Java| Spring-Hibernate | Certified Mulesoft Developer | Foodie | Nocturnal