当 exec returns 非零时,如何使骆驼路线失败?

How can I fail a camel-route when exec returns nonzero?

我正在使用这样的 Camel 路由处理文件:

    <route>
        <from uri="file:inbox?delete=true"/>
        <recipientList>
            <simple>exec://process.sh?args=inbox/${file:name}</simple>
        </recipientList>
        <log message="processed ${file:name}: ${body.stdout} ${body.stderr}"/>
    </route>

现在我希望路由在 process.sh 以非零退出代码结束时失败。我找到了 ${headers.CamelExecExitValue},但真的不知道如何处理它。

在上面的示例中,当 process.sh 失败时,文件不应被删除。在我的实际用例中,路由使用 JMS 队列中的文件,我希望文件保留在队列中。我认为这可以用 <transacted/> 来完成,但需要知道如何使路由失败。

我发现 How to define exception to be thrown through ref in Apache Camel 结合 CamelExecExitValue 让我可以这样中止:

    <route>
        <from uri="file:inbox?delete=true"/>
        <to uri="exec://process.sh"/>
        <choice>
            <when>
                <simple>
                    ${headers.CamelExecExitValue} != 0
                </simple>
                <throwException exceptionType="java.lang.RuntimeException" message="failed importing ${file:name}: ${body.stdout} ${body.stderr}"/>
            </when>
        </choice>
        <log message="processed ${file:name}"/>
    </route>

对我来说有点冗长,但工作正常。