即使在例外情况下也继续Mule(3.3)流程

时间:2012-09-26 17:12:31

标签: exception-handling error-handling esb mule

Mule 3.3.0
我有一个拆分有效载荷的流程 然后每个项目都通过一个自定义变换器,如果项目格式不正确,则会抛出异常 我有一个catch异常策略来将错误项记录到文件中 我希望流程继续处理剩余的项目,据我所知,这应该是预期的行为 问题是流量停止了。

我正在使用一个简单的测试文件附加一个简单的测试流程。 该文件是一个包含2行的csv文件,每行包含3个字段。 我使用groovy脚本,首先将文件分成行,然后将每行分成字段。 当字段格式错误时,我还使用groovy脚本来模拟异常。在这种情况下,如果字段是单词“goodbye”,它将抛出RuntimeException。 如果您测试此流程,您将看到异常后的其余字段将不会被处理(即在这种情况下记录)。 在这个特殊的例子中,捕获异常策略甚至不会触发。

输入文件:

hello,cruel,world
goodbye,cruel,world

TestFlow

TestFlow

TestCase.mflow

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns:file="http://www.mulesoft.org/schema/mule/file"
    xmlns:vm="http://www.mulesoft.org/schema/mule/vm" 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" version="CE-3.3.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd 
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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd ">

    <file:connector name="inputFileConnector" autoDelete="true"
        streaming="false" validateConnections="true" doc:name="File" fileAge="60000"
         readFromDirectory="#{systemProperties['user.home']}"/>
    <catch-exception-strategy name="Catch_Exception_Strategy">
        <logger message="!!!!! Exception Handler !!!!!" level="INFO" doc:name="Logger"/>
    </catch-exception-strategy>

    <flow name="TestCaseFlow1" doc:name="TestCaseFlow1">
        <file:inbound-endpoint path="#{systemProperties['user.home']}"
            responseTimeout="10000" doc:name="Input File" fileAge="100" connector-ref="inputFileConnector">
            <file:filename-regex-filter pattern="input.csv"
                caseSensitive="false" />
        </file:inbound-endpoint>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy">
                <scripting:text><![CDATA[return payload.split('\n');]]></scripting:text>
            </scripting:script>
        </scripting:component>
        <collection-splitter doc:name="Collection Splitter"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy">
                <scripting:text><![CDATA[return payload.split(',');]]></scripting:text>
            </scripting:script>
        </scripting:component>
        <collection-splitter doc:name="Collection Splitter"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy">
                <scripting:text><![CDATA[if (payload.equals('goodbye')) {
                            throw new java.lang.RuntimeException('Dang!');
                        }
                        return payload]]></scripting:text>
            </scripting:script>
        </scripting:component>
        <logger message=">>>>>>>>>>>>>>>>>> #[payload]" level="INFO" doc:name="Logger" />
    </flow>
</mule>

1 个答案:

答案 0 :(得分:4)

我首先要做的是分开你的主要流程。在每个collection-splitter放置一个JMS或VM outbound-endpoint之后,这样每条消息都有自己的线程,如果一个失败,其他消息就不会受到影响。

<flow name="flow1">
  <file:inbound-endpoint path="#{systemProperties['user.home']}"
      responseTimeout="10000" doc:name="Input File" fileAge="100" connector-ref="inputFileConnector">
      <file:filename-regex-filter pattern="input.csv"
      caseSensitive="false" />
  </file:inbound-endpoint>
  <byte-array-to-string-transformer doc:name="Byte Array to String"/>
  <scripting:component doc:name="Groovy">
      <scripting:script engine="Groovy">
      <scripting:text><![CDATA[return payload.split('\n');]]></scripting:text>
      </scripting:script>
  </scripting:component>
  <collection-splitter doc:name="Collection Splitter"/>
  <vm:outbound-endpoint path="toFlow2"/>
</flow>

 <flow name="flow2">
  <vm:inbound-endpoint path="toFlow2"/>
  <scripting:component doc:name="Groovy">
      <scripting:script engine="Groovy">
      <scripting:text><![CDATA[return payload.split(',');]]></scripting:text>
      </scripting:script>
  </scripting:component>
  <collection-splitter doc:name="Collection Splitter"/>
  <vm:outbound-endpoint path="toFlow3"/>
</flow>

 <flow name="flow3">
  <vm:inbound-endpoint path="toFlow3"/>
  <scripting:component doc:name="Groovy">
      <scripting:script engine="Groovy">
      <scripting:text><![CDATA[if (payload.equals('goodbye')) {
              throw new java.lang.RuntimeException('Dang!');
          }
          return payload]]></scripting:text>
      </scripting:script>
  </scripting:component>
  <logger message=">>>>>>>>>>>>>>>>>> #[payload]" level="INFO" doc:name="Logger" />
</flow>