2016-04-08 19 views
1

Belirli bir istisnada deve ayırıcıda döngü yapmayı nasıl durdurabiliriz? "stopOnException()", her özel durum için döngüyü durduruyor, ancak bunun yerine yalnızca belirli bazı özel durumlar için döngüyü durdurmak istiyorum. Ve istisna "HttpOperationFailedException" ise, yanıt koduna göre döngü yapmayı durdurmak istiyorum. Örneğin, yanıt kodu "500" ise, yürütmeyi durdurun ve yanıt kodu 404 ise yürütmeye devam edin.deve ayırıcı - belirli bir istisnada döngüyü durdurma

Mümkün mü?

Orijinal Soru atar

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"); 

Bean istisna:

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); 
} 

}

Çalışma Çözüm:

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"); 

cevap

1

Neden tabanlı özel bir özel durum yok üzerinde bir cevap kodu Bu bir seçenek. Temel olarak orijinal http istisnasını yakalayabilir, yanıt kodunu kontrol edebilir, özel durumunuzu atabilirsiniz. Rotanızı gönderir misiniz? Bu şekilde uygulanması kolaydır, sadece rotalarınızı nasıl düzenlediğinizi görmek istersiniz.

+0

Teşekkürler, işe yaradı! Ayrıca sorumu rota kodunu da dahil ederek düzenledim. – Shyam

+0

@Shyam Cheers, ama diğer tüm istisnaları bastırmak için bir tane daha yakalama bloğu eklemeniz gerekecek. Bu makalede, birkaç ay geriye dönük belgelenmiş istisna durumun ortaya çıkmasına yardımcı olabilir - http://bushorn.com/exception-handling-in-camel/. İyi şanslar . – gnanagurus

0

İstisnaları yakalayabilir ve bunlarla ne yapacağınıza karar verebilirsiniz. senin splitter İçinde:

<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

Cevabınız için teşekkür ederiz. Çözümünüzü denedim, ancak özel bir istisna yakaladım ve yakalama blokunda "dur" kullanmama rağmen splitter durmadı. – Shyam

0

Temelde biz hala durum oluştu zaman splitter durdurmak için "stopOnException" kullanmak gerekir. Ancak splitterin hangi istisnada kırılacağını kontrol etmek için "doTry..doCatch" bloğunu kullanabilir ve ilgili catch bloğundaki istisnayı tekrar geri alabilirsiniz. istisna http ilgili ve yanıt kodu buna göre o zaman hareket etmeye incelemek istiyoruz ise

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"); 

yapabilirsiniz çalışma çözümü vardır benim sorum.