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");
Teşekkürler, işe yaradı! Ayrıca sorumu rota kodunu da dahil ederek düzenledim. – Shyam
@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