Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Working With Object Store in Mule, Part 3

DZone's Guide to

Working With Object Store in Mule, Part 3

In this, the third and final post of this series, we will look at how to implement the Object Store in Anypoint Studio. Read on for more!

· Integration Zone ·
Free Resource

The Future of Enterprise Integration: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

In our previous posts, Working With Object Store in Mule, Part 1 and Working With Object Store in Mule, Part 2, we looked at the concept of Object Store and different operations supported by the Object Store. In this third and final post, we will look at how to implement the Object Store in Anypoint Studio.

In this example, we will perform the below operations on Object Store.

  1. Store the values in the Object Store.

  2. Retrieve the values from the Object Store.

  3. List all the keys in the Object Store.

  4. Remove the key from the Object Store

  5. Check whether the Object Store contains the given key or not.

Flows

Let’s walk through how to use the Object Store in the application. In this example, we are using the REST client to invoke the Mule flow and to trigger the Object Store operation.

Store Flow

In this flow, we will store the data in the Object Store by adding a key 'language' and 'type' to the Object Store. Both keys and values are hardcoded into the flow. We will use the tag <objectstore:store/> to store data :

Image title

Retrieve Flow

In this flow, we will retrieve values which we inserted into the Object Store from the store flow. We will use <objectstore:retrieve/> to retrieve any key value from the Object Store:

Image title

Retrievall Flow

In this flow, we will retrieve all the key from the Object Store of all the data that we inserted into it from store flow.We will use tag <objectstore:all-keys/> to retrieve all the keys:

Image title


Remove Flow

In this flow, we will delete the key 'type' in the Object Store which we added to the store flow. We will use <objectstore:remove/> tag to remove any key from the Object Store:

Image title

Contains Flow

In this flow, we will check whether the object store contains the given key or not. It returns true if the object store contains the key, or false if it doesn't. We will use the <objectstore:contains/> tag to check for the existence of any key in Object Store:

Image title

Code:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:objectstore="http://www.mulesoft.org/schema/mule/objectstore" 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.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd 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/objectstore http://www.mulesoft.org/schema/mule/objectstore/current/mule-objectstore.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="localhost" port="8083" basePath="/object" doc:name="HTTP Listener Configuration"></http:listener-config>
    <objectstore:config name="ObjectStore" partition="TechnologyStore" maxEntries="1000" persistent="true" doc:name="ObjectStore" />
    <flow name="Object-Store-Store-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/store" doc:name="HTTP" />
        <logger level="INFO" message="Adding language and type to Object store" doc:name="Logger" />
        <objectstore:store config-ref="ObjectStore" key="language" value-ref="#['Java Language']" doc:name="ObjectStore" />
        <objectstore:store config-ref="ObjectStore" key="type" value-ref="#['Programming']" doc:name="ObjectStore" />
        <set-payload value="language and type added to Object store" doc:name="Set Payload" />
    </flow>
    <flow name="Object-Store-Retrieve-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/retrieve" doc:name="HTTP" />
        <objectstore:retrieve config-ref="ObjectStore" key="language" doc:name="ObjectStore" />
    </flow>
    <flow name="Object-Store-Retrieveall-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/retrieveall" doc:name="HTTP" />
        <objectstore:all-keys config-ref="ObjectStore" doc:name="ObjectStore" />
        <object-to-string-transformer doc:name="Object to String" />
    </flow>
    <flow name="Object-Store-Remove-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/remove" doc:name="HTTP" />
        <objectstore:remove config-ref="ObjectStore" key="type" doc:name="ObjectStore" />
    </flow>
    <flow name="Object-Store-Contains-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/contains" doc:name="HTTP" />
        <objectstore:contains config-ref="ObjectStore" key="type" doc:name="ObjectStore" />
        <object-to-string-transformer doc:name="Object to String" />
    </flow>
    <flow name="Object-Store-ObjetStoreSize-Flow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/size" doc:name="HTTP" />
        <set-variable variableName="objectStoreSize" value="#[app.registry.ObjectStore.allKeys().size()]" doc:name="Variable" />
        <logger level="INFO" message="Size of object store is #[flowVars.objectStoreSize]" doc:name="Logger" />
    </flow>
</mule>

Hope this helps, thanks!

Keep Learning!

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
mule ,anypoint flows ,anypoint studio ,mule esb ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}