Invoking Flows From DataWeave Transformer
DataWeave can be used not only for data mapping, but also to invoke a Mule flow. This quick step-by-step tutorial will show you how.
Join the DZone community and get the full member experience.
Join For Free
1.0 Overview
We commonly understand the DataWeave transformer to be used for data mapping, but not many people know that you can also invoke a Mule flow inside of DataWeave. Figure 1.0 shows an overview of how this invocation works.
Figure 1.0.
Figure 1.0 depicts a conceptual execution flow control if a DataWeave were to call a secondary flow. The secondary flow could be a private flow (a flow with no inbound message processor) or it could be a conventional flow with an inbound message processor.
It wouldn't matter if the secondary flow has an inbound message processor or not, because if you were to invoke a flow from Datawave, the program execution flow control will bypass the inbound message processor, if any, and start immediately at the first message processors, the execution flow is depicted by the red arrows. The payload would obviously be modified by the secondary flow when it returns to the DataWeave.
DataWeave can only call flows. You can script DataWeave to call subflows, it will not error during startup of your Mule application, but it will have a runtime error if you try to execute the portion of the script where it tries to invoke a subflow.
2.0 Digging Deeper
As always, the best way to explore something is to do an experiment on it. I have created a test application to test the concepts out. Figure 2.0a depicts how my test application would look.
Figure 2.0a.
Upon starting up the application, you would be able to test it via Postman, with the following settings depicted in Figure 2.0b.
Figure 2.0b.
If the route parameter is set to “pflow,” DataWeave will route the call from Main Flow to Private Flow; if the route parameter is set to “sflow,” DataWeave will route the execution to the subflow.
If you pass in “pflow” as the parameter and the following payload (Figure 2.0c):
{
"Input" : "Test Input Payload"
}
You will get the following response payload.
Figure 2.0c.
If you set the route parameter to “sflow,” you will get the following exception in your console.
ERROR 2017-08-3018:37:09,227 [[dataweavecallingflow].HTTP_Listener_Configuration.worker.01] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message : org.mule.processor.chain.SubflowInterceptingChainLifecycleWrapper cannot be cast to org.mule.construct.Flow (java.lang.ClassCastException).
Payload : {
"Input" : "Test Input Payload"
}
Payload Type : java.lang.String
Element : /MainFlow/processors/2/1/0 @ dataweavecallingflow:dataweavecallingflow.xml:26 (Call Sub Flow)
Element XML : <dw:transform-message doc:name="Call Sub Flow">
<dw:set-payload>%dw 1.0%output application/json---lookup("SubFlow", payload.Input)</dw:set-payload>
</dw:transform-message>
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.ClassCastException: org.mule.processor.chain.SubflowInterceptingChainLifecycleWrapper cannot be cast to org.mule.construct.Flow
at com.mulesoft.weave.mule.function.FlowRefLookupFunctionValue.call(FlowRefLookupFunctionValue.scala:71)
at com.mulesoft.weave.engine.ast.functions.FunctionCallNode.doExecute(FunctionCallNode.scala:10)
at com.mulesoft.weave.engine.ast.ValueNode$class.execute(AstNode.scala:43
The following is the DataWeave code for calling a flow. It uses the lookup function in DataWeave. As I have said earlier, you can start your application without issues if you try to execute a subflow from DataWeave, but you will always get runtime errors.
%dw 1.0
%output application/json
---
lookup("PrivateFlow", payload.Input)
The following is the full Mule configuration file that was created to test the lookup function. The MuleSoft documentation does not really elaborate on the lookup function, hence I found the need to write this article to demystify it.
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" 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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="MainFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<byte-array-to-object-transformer doc:name="Byte Array to Object"/>
<logger message="#[message.inboundProperties.'http.query.params'.route == "pflow"]" level="INFO" doc:name="Logger"/>
<choice doc:name="Is Private Flow Route?">
<when expression="#[message.inboundProperties.'http.query.params'.route == "pflow"]">
<dw:transform-message doc:name="Call Private Flow">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
lookup("PrivateFlow", payload.Input)]]></dw:set-payload>
</dw:transform-message>
</when>
<otherwise>
<dw:transform-message doc:name="Call Sub Flow">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
lookup("SubFlow", payload.Input)]]></dw:set-payload>
</dw:transform-message>
</otherwise>
</choice>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="#["Error caught in MainFlow..."]" doc:name="Set Payload"/>
</catch-exception-strategy>
</flow>
<flow name="PrivateFlow">
<set-payload value="#[payload + " PrivateFlow Called"]" doc:name="Set Payload"/>
</flow>
<sub-flow name="SubFlow">
<set-payload value="#[payload + " SubFlow Called"]" doc:name="Set Payload"/>
</sub-flow>
</mule>
3.0 Conclusion
Invoking another flow from within MuleSoft DataWeave is not a common thing to do, but if you are doing it and have a use case for it, please do share it in the comments section of this article. There are obvious pros and cons of doing it, one of the cons is that it won't be apparent to the developer if a DataWeave transformer is invoking another flow, unless he or she looks into the DataWeave scripts.
Opinions expressed by DZone contributors are their own.
Comments