mulemule-studio

How to catch exceptions in the Mule foreach scope but keep the process going?


I am new to Mule ESB. I have created a simple flow that loops through a list of orders and calls the Magento API to update the order statuses one by one. My problem is that if there are any exception occurs in the foreach scope, the whole process tops. I tried to use the Exception Strategy to capture the exception and it did capture the exception. But how to resume the process? I didn't find much info with google search. Maybe I was doing something wrong with the flow. How do we normally handle this in Mule?

Here is my flow in xml.

    <flow name="Update_Magento_Order_Status_AU" doc:name="Update_Magento_Order_Status_AU" initialState="started">
    <poll doc:name="Poll">
        <fixed-frequency-scheduler frequency="10" timeUnit="MINUTES"/>
        <jdbc-ee:outbound-endpoint exchange-pattern="request-response" queryKey="GET_ORDERS_BY_STATUS_QUERY" queryTimeout="-1" connector-ref="DSEDatabase" doc:name="Get Orders By Status"/>
    </poll>
    <flow-ref name="ProcessOrderStastusUpdate" doc:name="Process Order Status Update"/>
</flow>
<flow name="ProcessOrderStastusUpdate" doc:name="ProcessOrderStastusUpdate">
    <foreach collection="#[payload]" doc:name="For Each">
        <component doc:name="Set Magento Order Status for Update">
            <singleton-object class="com.dse.esb.component.OrderStatusMapperComp">
                <property key="as400OrderStatuses" value="${as400.orderstatuses}"/>
                <property key="magentoOrderStatuses" value="${magento.orderStatuses}"/>
            </singleton-object>
        </component>
        <logger message="About to update Magento Order Status" level="INFO" doc:name="Logger"/>
        <magento:add-order-comment config-ref="Magento" comment="Updated by Mule ESB with AS400 order status: #[payload.TRNSTS]" orderId="#[payload.EPGORDNBR]" status="#[flowVars['magentoOrderStatus']]" doc:name="Update Magento Order Status"/>
    </foreach>
    <choice-exception-strategy doc:name="Choice Exception Strategy">
        <catch-exception-strategy doc:name="default">
            <logger message="Handle default exception" level="INFO" category="==============&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" doc:name="Logger"/>
        </catch-exception-strategy>
    </choice-exception-strategy>
</flow>

Solution

  • Use a private flow for the content of the for-each with its own exception strategy. THe exception will be handled in the private flow and the parent flow should be able to continue. Something like:

    <flow name="ProcessOrderStastusUpdate">
       <foreach collection="#[payload]" doc:name="For Each">
          <flow-ref name="privateFlow" />    
       </foreach>
    </flow>
    
    <flow name="privateFlow">
       <component doc:name="Set Magento Order Status for Update">
          <singleton-object class="com.dse.esb.component.OrderStatusMapperComp">
                 <property key="as400OrderStatuses" value="${as400.orderstatuses}"/>
                 <property key="magentoOrderStatuses" value="${magento.orderStatuses}"/>
          </singleton-object>
       </component>
       <logger message="About to update Magento Order Status" level="INFO" doc:name="Logger"/>
       <magento:add-order-comment config-ref="Magento" comment="Updated by Mule ESB with AS400 order status: #[payload.TRNSTS]" orderId="#[payload.EPGORDNBR]" status="#[flowVars['magentoOrderStatus']]" doc:name="Update Magento Order Status"/>
    
    
        <choice-exception-strategy doc:name="Choice Exception Strategy">
            <catch-exception-strategy doc:name="default">
                <logger message="Handle default exception" level="INFO" category="==============&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;" doc:name="Logger"/>
            </catch-exception-strategy>
        </choice-exception-strategy>
    </flow>