2016-04-08 29 views
1

如何停止駱駝分配器上的特定異常循環? 「stopOnException()」正在停止每個異常的循環,但是我想停止僅循環某些特定的異常。如果異常是「HttpOperationFailedException」,我想根據響應代碼停止循環。 例如,如果響應代碼是「500」,則停止執行,並且如果響應代碼是404,則繼續執行。駱駝分配器 - 停止特定異常循環

可能嗎?

原始的問題

from("timer:categoryRouter?delay=0") 
        .process(new Processor() { 
         @Override 
         public void process(Exchange exchange) throws Exception { 
          exchange.getIn().setBody("A,F,B,D,C"); 
         } 
        }) 
       // tell Splitter to use the aggregation strategy which handles and ignores exceptions 
       .split(body(), new MyIgnoreFailureAggregationStrategy()) 
        .stopOnException() 
        // log each splitted message 
        .log("Split line ${body}") 
        // and have them translated into a quote 
        .bean(WordTranslateBean.class) 
        // and send it to a mock 
        .to("mock:split") 
       .end() 
       // log the outgoing aggregated message 
       .log("Aggregated ${body}") 
       // and send it to a mock as well 
       .to("mock:result"); 

豆會拋出異常:

public class WordTranslateBean { 

private Map<String, String> words = new HashMap<String, String>(); 

public WordTranslateBean() { 
    words.put("A", "Camel rocks"); 
    words.put("B", "Hi mom"); 
    words.put("C", "Yes it works"); 
} 

public String translate(String key) throws HttpOperationFailedException { 
    if (!words.containsKey(key)) { 
     HttpOperationFailedException httpOperationFailedException = null; 
     if(key.equals("F")) { 
      httpOperationFailedException = new HttpOperationFailedException("uri",500,"Internal Server Error","location",null,"Key not a known word " + key); 
     } 
     else { 
      httpOperationFailedException = new HttpOperationFailedException("uri",404,"Resource Not Found","location",null,"Operation not supported on word " + key); 
     } 
     throw httpOperationFailedException; 
    } 
    return words.get(key); 
} 

}

工作液:

from("timer:categoryRouter?delay=0") 
        .process(new Processor() { 
         @Override 
         public void process(Exchange exchange) throws Exception { 
          exchange.getIn().setBody("A,F,B,D,C"); 
         } 
        }) 
       // tell Splitter to use the aggregation strategy which handles and ignores exceptions 
       .split(body(), new MyIgnoreFailureAggregationStrategy()) 
        .stopOnException() 
        // log each splitted message 
        .log("Split line ${body}") 
        // and have them translated into a quote 
        .doTry() 
         .bean(WordTranslateBean.class) 
         // and send it to a mock 
         .to("mock:split") 
        .doCatch(HttpOperationFailedException.class) 
         .process(new Processor() { 
          @Override 
          public void process(Exchange exchange) throws Exception { 
           HttpOperationFailedException e = (HttpOperationFailedException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT); 
           if(e.getStatusCode()!=404){ 
            throw e; 
           } 
          } 
         }) 
        .end() 
       .end() 
       // log the outgoing aggregated message 
       .log("Aggregated ${body}") 
       // and send it to a mock as well 
       .to("mock:result"); 

回答

1

你爲什麼不扔基於自定義異常上 一個響應碼?這是一種選擇。基本上你可以捕獲原始的http異常,檢查響應代碼,拋出你的自定義異常。你能張貼你的路線嗎?這種方式很容易實現,只是想看看你是如何組織你的路線。

+0

謝謝,它的工作!此外,我編輯我的問題,包括路線代碼。 – Shyam

+0

@Shyam乾杯,但我想你還需要再添加一個catch塊來抑制所有其他異常。這篇文章可能會有所幫助,以防萬一,幾個月前記錄異常處理 - http://bushorn.com/exception-handling-in-camel/。祝你好運 。 – gnanagurus

0

您可以捕捉異常並決定如何處理它們。裏面的分離器:

<doTry> 
    <!-- Your Splitter logic here --> 
    <doCatch> 
     <exception>java.lang.IllegalStateException</exception> 
     <log message="This exception happened here, but not a problem.."/> 
    </doCatch> 
    <doCatch> 
     <exception>java.io.IOException</exception> 
     <log message="Big problem here. STOPPING.."/> 
     <stop/> 
    </doCatch> 
    <doFinally> 
     <to uri="mock:finally"/> 
    </doFinally> 
</doTry> 
+1

感謝您的回答。我嘗試了你的解決方案,但是即使我發現了特定的異常並且在catch塊中使用了「stop」,分離器也沒有停止。 – Shyam

0

基本上,我們仍然需要使用「stopOnException」停止分流異常發生時。但是爲了控制拆分器應該中斷哪個異常,可以使用「doTry..doCatch」塊並在相應的catch塊中再次拋出異常。

from("timer:categoryRouter?delay=0") 
        .process(new Processor() { 
         @Override 
         public void process(Exchange exchange) throws Exception { 
          exchange.getIn().setBody("A,F,B,D,C"); 
         } 
        }) 
       // tell Splitter to use the aggregation strategy which handles and ignores exceptions 
       .split(body(), new MyIgnoreFailureAggregationStrategy()) 
        // log each splitted message 
        .log("Split line ${body}") 
        // and have them translated into a quote 
        .doTry() 
         .bean(WordTranslateBean.class) 
         // and send it to a mock 
         .to("mock:split") 
        .doCatch(HttpOperationFailedException.class) 
         .log("Ignore Exception") 
        .doCatch(IOException.class) 
         .throwException(new IOException()) 
        .doCatch(UnsupportedOperationException.class) 
         .log("Ignore Exception") 
        .end() 
       .end() 
       // log the outgoing aggregated message 
       .log("Aggregated ${body}") 
       // and send it to a mock as well 
       .to("mock:result"); 

如果異常有關,HTTP和要檢查響應代碼採取相應的行動,那麼你可以在我的問題,它有可行的解決方案。