Over a million developers have joined DZone.

Invoking Flows From DataWeave Transformer

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

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>
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"
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="" 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 doc:name="Call Sub Flow">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
lookup("SubFlow", payload.Input)]]></dw:set-payload>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<set-payload value="#[&quot;Error caught in MainFlow...&quot;]" doc:name="Set Payload"/>
<flow name="PrivateFlow">
<set-payload value="#[payload + &quot; PrivateFlow Called&quot;]" doc:name="Set Payload"/>

<sub-flow name="SubFlow">
<set-payload value="#[payload + &quot; SubFlow Called&quot;]" doc:name="Set Payload"/>

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.

mulesoft ,dataweave ,integration ,mule ,mule flow

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}