How do I implement IF in mulesoft

你说的曾经没有我的故事 提交于 2020-07-21 11:19:23

问题


I want to make a decision in a Mulesoft flow, and have looked at the Choice Flow Control. My problem, is that I want to do something if the condition is true, and nothing if it is false, something like:

if (condition == true)
   do some work

or, in probably incorrect xml:

<choice doc:name="call a subflow if the test is true">
    <when expression="#[flowVars.someVariable == True]">
        <flow-ref name="doSomething" doc:name="do another thing"/>
    </when>
</choice>

no else clause, and no default flow. How is this implemented in a Mulesoft flow? I could cheat, and throw a logging call into a default flow, but I would prefer not to.


回答1:


Unfortunately there is no simple 'if' processor in Mule. Choice with a dummy otherwise route or filters are the way to go for now.

There is a good discussion on this here: https://www.mulesoft.org/jira/browse/MULE-6129. THis has further links to possible enhancements such as an if/detour router.

Mule 4 UPDATE

In mule 4, you can now define a choice router with no need for an otherwise route. And filters no longer exist




回答2:


You can get close with an async or enricher scope and a filter. It's not as elegant as a true <if> processor (or a standalone <when>), but you don't need a wasted <logger> to satisfy the <otherwise>.

Async method (when you don't need the payload afterwards):

<async>
    <expression-filter expression="#[payload == 'red']" />
    <logger category="com.box.integration.experiment" message="That's my favorite color!" level="INFO" />
</async>

Enricher method (when you do):

<enricher target="#[variable:colorName]">
    <processor-chain>
        <expression-filter expression="#[payload == 'red']" />
        <set-payload value="vermillion" />
    </processor-chain>
</enricher>



回答3:


In a mule flow, when using a message router without setting a Processor in the section, and the MuleMessage does not match any of the clauses, an exception is thrown. To achieve a conditional behavior currently mule requires to set a dummy processor on the otherwise clause. One usability improvement would be letting the message be processed by the remaining part of the flow if no clause was matched and no processor provided https://www.mulesoft.org/jira/browse/MULE-6129



来源:https://stackoverflow.com/questions/29105709/how-do-i-implement-if-in-mulesoft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!