Contact Us 1-800-596-4880

Mule Object Stores

Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version.

An object store is a facility for storing objects in Mule. Mule uses object stores whenever it needs data to persist for later retrieval. Internally, Mule uses object stores in various filters, routers, and other message processors that need to store state between messages. In most cases, Mule creates and manages object stores automatically, so no user configuration is necessary.

To use the Object Store module in Anypoint Studio, you must first download the Object Store Connector from the Anypoint Exchange.

Use Cases

In most cases, Mule creates and manages object stores for you, so no configuration is necessary. However, you may explicitly configure an object store in the following cases:

  • When configuring an idempotent message filter or until a successful scope occurs.

  • When configuring a custom component that must use an object store to persist information.

  • When storing or retrieving information from a Mule flow through the Object Store module.

Mule provides two types of object stores:

  • In-memory store – Prior to Mule 3.5.0, in-memory store was the default. As of Mule 3.5.0, persistent store is the default. For more information, see "Object Stores and Clustering" in the Cache Scope document.

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

As of Mule 3.5.0, the default object store created for all new caching strategies supports cluster mode out of the box, with the exception of the cache scope which uses the old default caching strategy. For details, see the Object Stores and Clustering section in Cache Scope.

Adding Object Store Components

  1. Install the Object Store component available in Anypoint Exchange

    install+object+store+connector

    This starts an installation Wizard. Select "ObjectStore Connector (Mule 3.6+)", click "Next", then "Next" again. Accept the terms of the license agreement and click "Finish".

  2. After installing the Object Store Connector into studio, you can see it available in the Studio Palette.

  3. Drag it into a flow in your canvas. This creates a generic Object Store element.

    object store in flow
  4. Click it to access its properties editor.

    object store properties editor
  5. Click the green plus sign to create a global configuration element for it.

  6. Then select one of the available operations out of the drop down menu

    Operation Description

    All keys

    Returns a list of all the keys in the object store.

    Contains

    Checks whether the object store contains the given key.

    Dual store

    Stores a value using key, and also store key using value.

    Remove

    Remove the object for the respective key.

    Retrieve

    Retrieve an object from the object store and make it available in the specified property scope of a Mule Message.

    Retrieve store

    Retrieve and Store in the same operation.

    Retrieve with lock

    Retrieve the given object with lock from the object store and make it available in the specified property scope of a Mule Message.

    Store

    Stores an object in the object store.

  7. Then, depending on what operation you select, different fields are available.

XML Example

The following example demonstrates how to configure object stores in the following three situations:

  1. idempotent filter with an in-memory object store

  2. idempotent filter with a persistent object store

  3. Until a successful scope occurs with an in-memory object store

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


<!-- Global object store definition for a Listable Object Store, used in Flow 3 below. -->

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

    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>

<!--  Idempotent Filter with In Memory Object Store -->

    <flow name="Flow1_idempotentWithInMemoryStore" doc:name="Flow1_idempotentWithInMemoryStore">
        <http:listener config-ref="HTTP_Listener_Configuration" path="idempotentInMemory" doc:name="HTTP"/>
        <idempotent-message-filter idExpression="#[message.payload]" throwOnUnaccepted="true" storePrefix="Idempotent_Message" doc:name="Idempotent Message">
            <in-memory-store name="myInMemoryObjectStore" entryTTL="120" expirationInterval="3600" maxEntries="60000" />
        </idempotent-message-filter>
        <set-payload value="YAY!" doc:name="Set Payload" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="NAY!" doc:name="Set Payload" />
        </catch-exception-strategy>
    </flow>

<!--  Idempotent Filter with Persistent File Store -->

    <flow name="Flow2_idempotentWithTextFileStore" doc:name="Flow2_idempotentWithTextFileStore">
        <http:listener config-ref="HTTP_Listener_Configuration" path="idempotentTextFile" doc:name="HTTP"/>
        <idempotent-message-filter idExpression="#[message.payload]" throwOnUnaccepted="true" storePrefix="Idempotent_Message" doc:name="Idempotent Message">
            <simple-text-file-store
                name="mySimpleTextFileStore"
                directory="#[server.tmpDir + '/objectstore']"
                entryTTL="120"
                expirationInterval="3600"
                maxEntries="60000" />
        </idempotent-message-filter>
        <set-payload value="YAY!" doc:name="Set Payload" />
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <set-payload value="NAY!" doc:name="Set Payload" />
        </catch-exception-strategy>
    </flow>

<!--  Until Successful Scope with In Memory Object Store -->

    <flow name="Flow3_UntilSuccessfulWithListableObjectStore" doc:name="UntilSuccessfulWithListableObjectStore">
        <http:listener config-ref="HTTP_Listener_Configuration" path="hey" doc:name="HTTP"/>
        <until-successful objectStore-ref="myListableObjectStore" maxRetries="15" secondsBetweenRetries="1" doc:name="Until Successful">
            <processor-chain doc:name="Processor Chain">
                <message-filter throwOnUnaccepted="true">
                    <expression-filter expression="return Math.random() &lt; 0.1" doc:name="Expression" />
                </message-filter>
                <logger message="This eventually happens." doc:name="Logger" />
            </processor-chain>
        </until-successful>
        <set-payload value="Completed" doc:name="Set Payload" />
    </flow>

</mule>