2013-01-16 92 views
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中做到這一點?

回答

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> 
+0

謝謝,這是有效的。它發回一條消息,但這不是我期待的消息。我越來越 肥皂:服務器 不能存儲文件:目標\郵件\確認\無效\ A.XML 是否與將XML文件中的另一個文件夾後的異常是一個問題拋出? – Graham

0

如果你想在一個文件中的異常信息存儲在你的示例代碼,你需要將其轉換爲字符串。否則,您將得到另一個關於文件的異常,因爲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> 
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>