Coova
Coova

Reputation: 1858

Can Multiple Requests overwrite Flow Variables currently in use?

I am having trouble understanding whether or not I am using the correct variable in the context of my flows.

I have a flow that is exposed via an HTTP Endpoint, in this flow I store data in regards to the incoming request and then proceed to use that Flow Variable containing the data through the flow/subflows.

My question is, if I have 2 request come in at the same time, will they basically be overwriting the flow-variable causing issues later in the flow chain?

I want to avoid request 1 setting the Flow Variable, then processing data and continuing to reference the Flow Variable which has been overwritten with data from request 2.

Is this how this would work? Are each entry into the flow independent of one another?

Upvotes: 0

Views: 834

Answers (3)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8321

"My question is, if I have 2 request come in at the same time, will they basically be overwriting the flow-variable causing issues later in the flow chain?"
Yes that each correct, with each new request new flow instance, will be created and the variable will be initialized with every new request.
Each request are independent to each other until and unless you use some kind of storage like Object Store to retain the flow variable value from where the request left.
Using memory store like Object store you can retain variable values with each new request otherwise each new request will create new variable instance in the flow

Upvotes: 0

Yevgeniy
Yevgeniy

Reputation: 2694

if I have 2 request come in at the same time, will they basically be overwriting the flow-variable causing issues later in the flow chain

No. Variables are stored in the message. For every HTTP-request a new message will be created.

you can easily try it out yourself with the following application:

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

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
    <flow name="foo">
        <http:listener config-ref="user-httpListenerConfig" path="/{test}" doc:name="HTTP"/>
        <set-variable variableName="testVariable" value="#[message.inboundProperties['http.uri.params']['test']]" doc:name="testVariable"/>
        <foreach collection="#[[1,2,3,4,5,6,7,8,9,0]]" doc:name="For Each">
            <logger message="#[flowVars.testVariable]" level="INFO" doc:name="Logger"/>
            <scripting:component doc:name="Groovy">
                <scripting:script engine="Groovy">
                <![CDATA[Thread.sleep(1000)
return payload]]></scripting:script>
            </scripting:component>
        </foreach>
    </flow>
</mule>

this is what this application looks like in AnypointStudio: enter image description here

just call http://localhost:8081/foo from your browser in one tab and http://localhost:8081/bar in another tab. you will see both foo and bar printed alternating in your logs.

Upvotes: 1

Alagappan
Alagappan

Reputation: 66

Flow variables are instance variables and its created every time for a new request.So, multiple request cannot override the flow variables as every request has their own instance variables.

Upvotes: 0

Related Questions