Apache camel 驼峰分割器-停止特定异常上的循环
我们如何停止在特定异常的驼峰分割器上循环? “stopOnException()”正在停止每个异常的循环,但我只想停止某些特定异常的循环。如果异常为“HttpOperationFailedException”,我希望停止基于响应代码的循环。 例如,如果响应代码为“500”,则停止执行,如果响应代码为404,则继续执行 可能吗 原始问题Apache camel 驼峰分割器-停止特定异常上的循环,apache-camel,Apache Camel,我们如何停止在特定异常的驼峰分割器上循环? “stopOnException()”正在停止每个异常的循环,但我只想停止某些特定异常的循环。如果异常为“HttpOperationFailedException”,我希望停止基于响应代码的循环。 例如,如果响应代码为“500”,则停止执行,如果响应代码为404,则继续执行 可能吗 原始问题 from("timer:categoryRouter?delay=0") .process(new Processor()
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:
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);
}
为什么不根据响应代码抛出一个自定义异常呢?这是一个选择。基本上,您可以捕获原始http异常,检查响应代码,抛出自定义异常。你能公布你的路线吗?这种方式很容易实现,只想看看你是如何组织路线的 您可以捕获异常并决定如何处理它们。在拆分器内部:
<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>
java.lang.IllegalStateException
java.io.IOException
基本上,我们仍然需要使用“StopOneException”在异常发生时停止拆分器。但是为了控制拆分器应该在哪个异常上中断,可以使用“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相关,并且希望检查响应代码以采取相应的行动,那么您可以查看我的问题,该问题具有有效的解决方案。谢谢,它有效!!我还编辑了我的问题以包含路线代码。@Shyam干杯,但我想您还需要添加一个catch块,以抑制所有其他异常。这篇文章可能有助于以防万一,几个月前记录在案的异常处理。祝你好运。谢谢你的回答。我尝试了您的解决方案,但它没有停止拆分器,即使我捕获了特定的异常并在catch块中使用了“stop”。
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");