1
votes

I have a mule flow which is a SOAP web service client (Mule Flow - A). This client call will be executed every 30 minutes to retrieve a unique session key from a 3rd party. I have other mule flows (running as separate mule applications) which need to refer this session variable in their flows. My question is - Is there a way to persist the session key retrieved by Mule Flow A in the mule server memory, so that other mule applications can access it during run-time? It is possible to save the data somewhere in a file or a database and retrieve it, but I would like to check whether this can be done otherwise before concluding with a solution.

Thanks!

Updated Sample Mule Flow for Shared Object Store:

Flow 1:

<?xml version="1.0" encoding="UTF-8"?>

<mule 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" version="EE-3.4.0" 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/1.0/mule-objectstore.xsd">

    <spring:beans>
        <spring:bean id="myListableObjectStore" class="org.mule.util.store.SimpleMemoryObjectStore"/>
    </spring:beans>
    <objectstore:config name="ObjectStore" objectStore-ref="myListableObjectStore" doc:name="ObjectStore"/>
    <flow name="objectstoreFlow1" doc:name="objectstoreFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8083" doc:name="HTTP"/>
        <message-filter doc:name="Filter favicon">
        <not-filter>
            <wildcard-filter pattern="/favicon.ico" caseSensitive="true"/>
        </not-filter>
        </message-filter>
        <logger message="#[message.InboundProperties.key]" level="INFO" doc:name="Logger"/>
        <objectstore:store config-ref="ObjectStore" key="#[message.inboundProperties.key]" value-ref="#[message.inboundProperties.value]" doc:name="ObjectStore"/>

    </flow>
</mule>

Mule Flow 2 - To retrieve the value

<?xml version="1.0" encoding="UTF-8"?>

<mule 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" version="EE-3.4.0" 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/1.0/mule-objectstore.xsd">

    <spring:beans>
        <spring:bean id="myListableObjectStore" class="org.mule.util.store.SimpleMemoryObjectStore"/>
    </spring:beans>
    <objectstore:config name="ObjectStore" objectStore-ref="myListableObjectStore" doc:name="ObjectStore"/>
    <flow name="MemoryTest2Flow1" doc:name="MemoryTest2Flow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8084" doc:name="HTTP"/>
                <message-filter doc:name="Filter favicon">
        <not-filter>
            <wildcard-filter pattern="/favicon.ico" caseSensitive="true"/>
        </not-filter>
        </message-filter>
        <objectstore:retrieve config-ref="ObjectStore" key="#[message.inboundProperties.key]" doc:name="ObjectStore"/>
  <logger level="ERROR" doc:name="Logger"/>
    </flow>
</mule>

Will I be able to retrieve the stored object in the first flow as part of the second mule flow using the same key?

4

4 Answers

2
votes

Yes, you can do all of this with Mule.

You will use the Quartz scheduler endpoint to call your 3rd party, presumably via http SOAP or REST.

You can put the newly acquired key in nearly any database of your choosing using the mule JDBC endpoint or you could use the mule-module-objectstore to hold your key in memory or be backed by file persistence.

You can then devise a retrieval method for your dependant services to get they key from your chosen location using (JDBC or objectstore). I dont see a compelling reason to use session vars in this case as it sounds like there is only one key, not one per session.

0
votes

You can make use of mule registry

To set the value

<scripting:script engine="Groovy">
<![CDATA[muleContext.getRegistry().registerObject("${Key}", new String(flowVars.value))]]>
            </scripting:script>
</scripting:component>

To read the value

 <scripting:script engine="Groovy"><![CDATA[payload = muleContext.getRegistry().get("${Key}")]]></scripting:script>

References: https://docs.mulesoft.com/mule-user-guide/v/3.6/storing-objects-in-the-registry https://gist.github.com/vejaay/44545f738d511b6d6b9e591c8784d677

0
votes

Another way to achieve the solution.

If the flows are in same application then you can use the same objectstore in both the flows.

If the flows are in different application then you can define the objectstore at domain project level e.g. <spring:beans> <spring:bean id="myobjectstore" class="org.mule.util.store.SimpleMemoryObjectStore"/> </spring:beans>

You can then refer to the object store in both the applications as below <objectstore:config name="ObjectStore__Connector" entryTtl="0" expirationInterval="0" maxEntries="0" doc:name="ObjectStore: Connector" objectStore-ref="myobjectstore"/>

This will ensure that both the application have access to objectstore.

0
votes
For this case, choose to use default object store is simpler 

Each Mule runtime includes these three spring bean instances:
_defaultInMemoryObjectStore
_defaultUserObjectStore     // if you want to use persistent option
_defaultTransientUserObjectStore

for example:
<objectstore:config name="ObjectStore" objectStore-ref="myListableObjectStore" doc:name="ObjectStore"/>

<objectstore:config name="ObjectStore" objectStore-ref="_defaultInMemoryObjectStore" doc:name="ObjectStore"/>