2

我在骆驼有以下路线

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>             
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>

当 XML 文件没有像这篇文章中那样通过验证时,我想收到错误消息

http://camel.465427.n5.nabble.com/XML-Validation-getting-to-the-error-messages-using-Camel-td4768229.html

但是我如何在 Spring DSL 中做到这一点?

4

3 回答 3

2

就像您引用的线程中的克劳斯状态一样:

异常原因 = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);

所以,这条路线应该保存你的例外:

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>     
            <transform>
               <simple>${property.CamelExceptionCaught}</simple>
            </transform 
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>
于 2013-01-16T16:01:17.790 回答
1

如果您想将异常信息存储在示例代码中的文件中,则需要将其转换为字符串。否则,您会得到另一个关于文件无法存储的异常,因为 Camel 无法将模式验证异常转换为 java.io.inputStream。

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>     
            <transform>
               <simple>${property.CamelExceptionCaught}</simple>
            </transform>
            <transform>
               <simple>${bodyAs(String)}</simple>
            </transform>
            <to uri="file:target/messages/validation/invalid?fileName=a.xml"/>
       </doCatch>
    </doTry> 
</route>
于 2013-04-26T20:38:58.810 回答
1

您可以在一个文件中发送异常,在另一个文件中发送错误的 xml。

<route>
    <from uri="target/in"/>
    <doTry> 
        <to uri="validator:schema.xsd"/>
        <to uri="file:target/messages/validation/valid?fileName=a.xml"/>
        <doCatch> 
            <exception>org.apache.camel.ValidationException</exception>
            <setHeader headerName="CamelOverruleFileName">
                <simple>${file:onlyname.noext}.${date:now:yyyyMMdd_HHmmssSSS}.xml</simple>
            </setHeader>
            <to uri="file:target/messages/validation/invalid/"/>
            <setBody>
                <simple>Got "${exception.message}" with this stack\n${exception.stacktrace}\n${body}</simple>
            </setBody>
            <setHeader headerName="CamelOverruleFileName">
                <simple>${file:onlyname.noext}.${date:now:yyyyMMdd_HHmmssSSS}.xml.error</simple>
            </setHeader>
            <to uri="file:target/messages/validation/invalid/"/>
        </doCatch>
    </doTry> 
</route>
于 2015-02-10T22:46:07.990 回答