How to Use ObjectStore in Mule
A developer provides a quick and easy tutorial on how to work with ObjectStore from your Mule palette to create persistent data.
Join the DZone community and get the full member experience.Join For Free
Object stores are used to store an object. Mule uses object stores whenever it needs to persist data for later retrieval. Mule uses object stores internally by utilizing a key value pair combination. It is already available in the Mule palette – from there we can directly use it. If it is not available in your Mule palette, you can get it from the Mule Exchange Portal.
2. Operations Available
An object store contains the following operations in the form of connectors in the Mule palette:
- Clear: Removes all the contents in the store.
- Contains: Returns whether the key is present or not.
- Remove: Removes the values associated to the given key.
- Retrieve: Retrieves the stored data for the given key.
- Retrieve all: Retrieves all the key value pairs in the
- Retrieve all keys: Returns a List containing all keys that the
- Store: Stores the given value using the given key.
3. Types of ObjectStore
- Persistent: This is the default option for the
ObjectStore. By using this option, stored data stays in the system even if the system or its runtime is down.
- In-Memory: Though this functionality is not available explicitly, it can be selected. By not selecting the persistent option,
ObjectStore, by default, becomes in-memory. This means that data is available only when the runtime is up.
4. Implementing a Demo ObjectStore (Storing and Retrieval)
- Drag and drop the
ObjectStore: Storeconnector from the Mule Palette.
- Configure the connector properties as per the screenshot below.
- Create global configurations as per the screenshot below
- Click Ok and your
ObjectStore: Storeconnector is ready!
- Drag and drop the
ObjectStore: Retrieveconnector from the Mule palette.
- Retrieve the data from
ObjectStoreby using a key.
- Check that the data is using a set payload or logger.
- Testing: Hit API.
Opinions expressed by DZone contributors are their own.