Tuesday, August 29, 2017

Invoking Flows from MuleSoft DataWeave Transformer

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 and inbound message processor.


It wouldn't matter if the secondary flow have 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 data weave to call subflows, it will not error during startup of your mule application, but it will have 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 like.


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 route parameter is set to “pflow” dataweave will route the the call from main flow to Private Flow, if route parameter is set to “sflow”, dataweave will route the the execution to 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-30 18: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


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 as described previously.


The following is the datawave code for calling a flow, it uses the lookup function in dataweave.
%dw 1.0
%output application/json
---
lookup("PrivateFlow", payload.Input)


The mulesoft documentation do not really elaborate on the lookup function, hence I find the need to write this article to demystify it.


The following is the full mule configuration file that was created to test the lookup function.
<?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 == &quot;pflow&quot;]" level="INFO" doc:name="Logger"/>
       <choice doc:name="Is Private Flow Route?">
           <when expression="#[message.inboundProperties.'http.query.params'.route == &quot;pflow&quot;]">
               <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="#[&quot;Error caught in MainFlow ...&quot;]" doc:name="Set Payload"/>
       </catch-exception-strategy>
   </flow>
   <flow name="PrivateFlow">
       <set-payload value="#[payload + &quot; PrivateFlow Called&quot;]" doc:name="Set Payload"/>

   </flow>
   <sub-flow name="SubFlow">
       <set-payload value="#[payload  + &quot; SubFlow Called&quot;]" 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 look into the dataweave scripts.