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.   



9 comments:

  1. awesome post presented by you..your writing style is fabulous and keep update with your blogs.

    Mulesoft online course hyderabad

    ReplyDelete
  2. Hi There,


    I love all the posts, I really enjoyed.
    I would like more information about this, because it is very nice., Thanks for sharing.


    Earlier this year, we started a new blog series, highlighting great contributions from Mule Soft Developers that are willing to share knowledge with the community. February was another busy month for Mule developers, and we want to share some of the amazing work they have been doing for the community.


    Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.

    Regards,
    Morgan

    ReplyDelete
  3. Hello There,

    Gratitude for putting up this prolific article! You truly make everything a cake walk. Genuinely good stuff, saving time and energy.
    MuleSoft Developer Meetups across the world had a very busy February! In fact, we had 2 Meetups per week, and four groups – Tampa, Cape Town, Charlotte, and the Online group – held their very first events. The Online group, which provides a virtual Meetup to anyone across the world, was particularly successful!
    So, what were Meetup leaders up to in the past few weeks? Read below to find out the top highlights from our 9 developer Meetups in February as well as insights into upcoming Meetups this month.
    Very useful article, if I run into challenges along the way, I will share them here.

    Shukran,
    Telsa

    ReplyDelete
  4. Hi There,


    You make learning and reading addictive. All eyes fixed on you. Thank you being such a good and trust worthy guide.

    I am excited to announce a new guide format for content that helps make understanding Any point Platform and implementing typical integrations patterns much easier. With any point Platform, an organization can not only integrate any two systems together, but can also deploy, and manage those applications all from within a single platform.
    The goal of these guides is to teach you — the developer — how to wield the platform and gain a meaningful understanding on how to achieve success
    with your own projects

    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.


    MuchasGracias,
    Lisa

    ReplyDelete
  5. Hi There,


    Amaze! I have been looking bing for hours because of this and i also in the end think it is in this article! Maybe I recommend you something helps me all the time?

    I started using Mulesoft Training blog for my training practice.
    For the last several years, microservices has been an important trend in IT architecture. Technology consulting firm Thoughtworks has declared that “a microservices architecture as programming model” is one of the four rising trends of 2017, whereas others in the press are expressing their endorsement of microservices––making architects and IT executives feel a fear of missing out on the next exciting trend.

    I am so grateful for your blog. Really looking forward to read more.


    Thank you,
    Morgan

    ReplyDelete