Camel验证错误消息

时间:2013-01-16 14:01:46

标签: validation exception apache-camel

我在Camel中有以下路线

<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中做到这一点?

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

如果要将异常信息存储在文件中,就像在示例代码中一样,则需要将其转换为String。否则,您将获得有关无法存储文件的另一个异常,因为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>

答案 2 :(得分:0)

您可以在一个文件中发送异常,在另一个文件中发送错误的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>