Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Mule Object Stores

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.

object store in 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, available as an extension.

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


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?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>

See Also