FlowVars do not get lost when you call another flow by using flow-ref , but if you call another flow by vm or any other transport , then the flow variable will be lost and you will not be able to find it in the other flow . SessionVars will be present in each and every flow till the execution of the call . This example also demonstrates how can we write java code in our mule file using groovy component and expression component . Make sure to import the packages of the class which you intend to use , otherwise use fully qualified class name in your code .
<http:listener-config port="8081" host="0.0.0.0"
name="myRequestConfig" doc:name="HTTP Listener Configuration">
<http:worker-threading-profile
maxThreadsActive="15" />
</http:listener-config>
<flow name="foreachexample">
<http:listener path="/foreach/test" config-ref="myRequestConfig"
allowedMethods="GET" doc:name="HTTP"/>
<logger message="RUNNNING poller ra " level="ERROR"/>
<expression-component>
java.util.ArrayList list = new java.util.ArrayList();
list.add("abc");
payload = list;
</expression-component>
<set-session-variable value="true" variableName="changeMe" doc:name="Session Variable" />
<set-variable value="true" variableName="myFlowVar" doc:name="Flow Variable" />
<foreach >
<logger message="fdsf" level="ERROR"/>
<flow-ref name="subFlowTest" />
<flow-ref name="flow2" />
<vm:outbound-endpoint path="/opt"/>
</foreach>
<vm:outbound-endpoint path="/opt"/>
<logger message="changeme : #[sessionVars.changeMe]" level="ERROR"/>
</flow>
<sub-flow name="subFlowTest">
<logger message="changeme flow var in sub flow : #[flowVars.myFlowVar]" level="ERROR"/>
<logger message="changeme session var in sub flow: #[sessionVars.changeMe]" level="ERROR"/>
<scripting:component doc:name="sleep">
<scripting:script engine="groovy">
<scripting:text><![CDATA[
java.lang.Thread.sleep(5000);]]></scripting:text>
</scripting:script>
</scripting:component>
</sub-flow>
<flow name="flow2">
<vm:inbound-endpoint path="/opt"></vm:inbound-endpoint>
<logger message="changeme session var in flow 2: #[sessionVars.changeMe]" level="ERROR"/>
<logger message="changeme flow var in flow 2: #[flowVars.myFlowVar]" level="ERROR"/>
</flow>
This is simple example in mule demonstrating use of SessionVar , FlowVar , SubFlow , Vm , ForEach ,Groovy and Expression Component .