Implementing Object Store With Mule ESB
Learn how you can install and implement the object store connector in Mule from the Anypoint exchange with the steps in this tutorial.
Join the DZone community and get the full member experience.
Join For Free1.0 Overview
Object stores are used to store an object. Mule uses object stores whenever it needs data persistent for later retrieval. Mule uses object stores internally for various filters, routers, and other processors that need to store state between messages. By default, the object store connector not present in Mule palette. So you can install it from Anypoint exchange.
2.0 Object Store Operations
The object store connector can perform various operations like Contains, Dual store, Remove, Retrieve, Retrieve all keys, Retrieve and Store and Store.
Contains is used to verify if given key is present in the object store.
Dual store stores a value using a key and also stores a key using a value.
Remove is to remove the respective key from the object store.
Retrieve is used to retrieve an object from the object store and make it available in the specified property scope of a Mule Message.
Retrieve all keys will return a list of all keys present in the object store.
Retrieve and Store is used to retrieve and store an object in the same operation.
Store is used to store the object in an object store.
3.0 Types of Object Store
Mule provided two types of object store as follows
In-memory store: This allows you to store objects in memory at runtime. Generally, objects are lost when runtime is shutdown.
Persistent store: Mule persists data when an object store is explicitly configured to be persistent. Mule creates a default persistent store in the file system.
4.0 Object Store Use Cases
OAuth returns the token during an authorization request and this token needs to send with every request and it has expiration period (e.g. 2 hours). You can store the token in an object store so another flow or Mule application can retrieve that token from the object store to send requests to the client. Every two hours, a new token is generated and can be stored in the object store.
It can be used when configuring an idempotent filter or until successful scope.
It can be used when configuring a custom component that must use an object store to persist information.
It can be used when storing or retrieving information from a Mule flow through the Object Store module.
5.0 Installing Object Store Connector
The Anypoint Object Store Connector enables you to access to default in-memory object stores, custom object stores and persistent object stores. Using this connector, you can create instant connectivity between object stores and ease the manipulation of object stores in Mule.
If the object store connector is not found in the Anypoint Studio Mule palette, it can be installed from the Anypoint exchange. You can connect to the Anypoint exchange from Anypoint Studio and install the object store connector. Just you need to accept the license agreement and at the end it will ask to restart the Anpoint studio to complete installation.
6.0 Implementing the Idempotent Filter With In-Memory Object Store
Place the HTTP Listener in the message source region and configure it.
Drag and drop the Idempotent Filter in the message processor and configure the object store by clicking on the add button and selecting core:in-memory-store.
3. Configure Max Entries, Entry TTL, and Expiration Interval. Click Finish.
Entry Ttl is Time-To-Live for stored values in milliseconds. "Max Entries” and “Expiration Interval” are mandatory for using this param. You must either provide all Entry TTL, Max Entries, and Expiration Interval, or none of them.
- Expiration interval specifies the expiration check in milliseconds.
- Max Entries specifies the maximum number of entries.
4. Configure the Id Expression (i.e. MEL to identify the duplicate message on basis of the expression defined) and check Throw on Unaccepted (it will give control to error handling when a duplicate message is received.)
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="idempotent-filter-object-storeFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/idempotent" allowedMethods="POST" doc:name="HTTP"/>
<idempotent-message-filter idExpression="#[json:name]" throwOnUnaccepted="true" storePrefix="Idempotent_Message" doc:name="Idempotent Message">
<in-memory-store name="InMemoryIdempotentFilter" maxEntries="60000" entryTTL="1200" expirationInterval="3600"/>
</idempotent-message-filter>
<set-payload value="Message Accepted." doc:name="Set Payload"/>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="Message Unaccepted." doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
</mule>
7.0 Implementing the Idempotent Filter With Persistent Object Store
- Place the HTTP Listener in the message source region and configure it.
Drag and drop the Idempotent Filter in the message processor and configure the object store by clicking on the add button and selecting core:simple-text-file-store.
3. Configure Max Entries, Entry TTL, Expiration Interval, and Directory (Directory is a file persistent store or file location where the object will be saved). Click Finish.
4. Configure the Id Expression (i.e. MEL to identify the duplicate message on basis of the expression defined) and check Throw on Unaccepted (it will give control to error handling when a duplicate message is received.)
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="idempotent-filter-object-storeFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/idempotent" allowedMethods="POST" doc:name="HTTP"/>
<idempotent-message-filter idExpression="#[json:name]" throwOnUnaccepted="true" storePrefix="Idempotent_Message" doc:name="Idempotent Message">
<simple-text-file-store name="InPersistentObjectStore" maxEntries="60000" entryTTL="1200" expirationInterval="3600" directory="src/test/resources/objectstore"/>
</idempotent-message-filter>
<set-payload value="Message Accepted." doc:name="Set Payload"/>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="Message Unaccepted." doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
</mule>
8.0 Storing and Retrieving Custom Objects From the Object Store
8.1 Storing Objects in the Object Store
1. Place the HTTP Listener in the message source region and configure it. Here, we are expecting the query parameter and are going to store that query parameter in the object store.
2. Now, drag and drop the object store into the message processor and configure the object store connector configuration by clicking on the add button.
Partition is name of the default In-Memory or persistent object store.
- Object Store Reference specifies the Object Store spring bean instance.
3. Select the operation to Store for storing the object. Provide the Key and Value Reference (i.e. Key=EmployeeID, ValueReference=#[message.inboundPrperties.’http.query.params.ID]).
8.2 Retrieving Objects From the Object Store
To retrieve the object from the object store, select the operation Retrieve and provide the key that you have used during the store operation (i.e. Key=EmployeeID). In case the key does not exist, it will throw an exception.
8.3 Mule Flow [Code]
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:objectstore="http://www.mulesoft.org/schema/mule/objectstore" xmlns:http="http://www.mulesoft.org/schema/mule/http" 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.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/objectstore http://www.mulesoft.org/schema/mule/objectstore/current/mule-objectstore.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<objectstore:config name="ObjectStore__Connector" partition="Emloyee" entryTtl="1200" expirationInterval="3600" maxEntries="60000" persistent="true" doc:name="ObjectStore: Connector"/>
<flow name="object-store-appFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/object" allowedMethods="POST" doc:name="HTTP"/>
<objectstore:retrieve-and-store config-ref="ObjectStore__Connector" key="EmployeeID" storeValue-ref="#[message.inboundPrperties.'http.query.params'.ID]" doc:name="ObjectStore"/>
</flow>
<flow name="object-store-appFlow1">
<http:listener config-ref="HTTP_Listener_Configuration" path="/retrieve" allowedMethods="GET" doc:name="HTTP"/>
<objectstore:retrieve config-ref="ObjectStore__Connector" key="EmployeeID" doc:name="ObjectStore"/>
</flow>
</mule>
8.4 Testing
You can use Postman to test the application. Please go through the below video and it will tell you how to perform testing for this Mule flow.
9.0 Object Store With Mule ESB [Video]
10.0 Conclusion
Object stores are very useful when you want to store the object and access it within or across the application. Objects can be stored in memory or a persistent file store.
Now you know how to use object stores in your Mule flow.
Opinions expressed by DZone contributors are their own.
Comments