本文主要是介绍Camel异常策略匹配逻辑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Camel异常策略匹配逻辑
- route 优先级大于 context
- 匹配逻辑
- 注意区分包装与继承
- 获取异常包装层级
- 遍历异常匹配处理策略
- 获取最终的匹配策略
- 代码
- Test Code
route 优先级大于 context
优先匹配 route 级别的异常策略, 然后匹配 context 级别的异常策略;
匹配逻辑
注意区分包装与继承
包装:ConnectException异常抛出后被包装成OrderFailedException异常,然后OrderFailedException异常又被包装成RuntimeCamelException异常,但是他们之间并不存在继承关系;
org.apache.camel.RuntimeCamelException (wrapper by Camel)
+ com.mycompany.OrderFailedException+ java.net.ConnectException
继承:而RuntimeCamelException继承RuntimeException异常,RuntimeException集成Exception异常
org.apache.camel.RuntimeCamelException
+ java.lang.RuntimeException+ java.lang.Exception
获取异常包装层级
以上述示例做参考,执行完该方法后,获取的异常列表为:
java.net.ConnectException
com.mycompany.OrderFailedException
org.apache.camel.RuntimeCamelException
public static Iterable<Throwable> createExceptionIterable(Throwable exception) {List<Throwable> throwables = new ArrayList<>();Throwable current = exception;// spool to the bottom of the caused by treewhile (current != null) {throwables.add(current);current = current.getCause();}// 此处有反转动作!!!Collections.reverse(throwables);return throwables;
}
遍历异常匹配处理策略
获取异常继承层级的递归算法:
private static int getInheritanceLevel(Class<?> clazz) {if (clazz == null || "java.lang.Object".equals(clazz.getName())) {return 0;}return 1 + getInheritanceLevel(clazz.getSuperclass());
}
判断异常与策略是否匹配的逻辑,以RuntimeCamelException异常匹配以下策略为例:
onException(Exception.class).maximumRedeliveries(1).redeliveryDelay(5000);
下述方法中,clazz 为 Exception.class, exception 为 RuntimeCamelException.class。
- 首先,判断RuntimeCamelException是否为Exception的实例;
- 然后,判断RuntimeCamelException与Exception是否为同一个类;
- 如果是同一个类则为严格匹配;
- 如果不为同一个类,则获取RuntimeCamelException与Exception之间的间隔层级
- 如果间隔层级比现有的间隔层级小,则更新获选异常策略和异常层级信息
if (filter(type, clazz, exception)) {// must matchif (!matchesWhen(type, exchange)) {LOG.trace("The type did not match when: {}", type);continue;}// exact match then breakif (clazz.equals(exception.getClass())) {candidate = type;candidateDiff = 0;break;}// not an exact match so find the best candidateint level = getInheritanceLevel(clazz);int diff = targetLevel - level;if (diff < candidateDiff) {// replace with a much better candidatecandidate = type;candidateDiff = diff;}
}
获取最终的匹配策略
如果完全匹配则返回完全匹配的异常策略;
否则返回间隔层级小的异常策略;
代码
org.apache.camel.processor.errorhandler.DefaultExceptionPolicyStrategy#getExceptionPolicy
Test Code
@Test
public void testOnExceptionDirectMatch() throws Exception {context.addRoutes(new RouteBuilder() {@Overridepublic void configure() throws Exception {context.setTracing(true);onException(Exception.class).maximumRedeliveries(6);onException(OrderFailedException.class).maximumRedeliveries(3);from("direct:order").onException(Exception.class).maximumRedeliveries(10).end().bean(OrderServiceBean.class, "handleOrder");}});context.start();try {template.requestBody("direct:order", "ActiveMQ in Action");fail("Should throw an exception");Thread.sleep(10000l);} catch (CamelExecutionException e) {assertIsInstanceOf(OrderFailedException.class, e.getCause());}
}
在候选异常策略里包含两个异常策略,一个是完全匹配,来自global(camel context 级别),另外一个来自route(路由内配置);最终会选择严格匹配的OrderFailedException匹配策略。
最终结果输出如下:
handler order!
handler order!
handler order!
handler order!
这篇关于Camel异常策略匹配逻辑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!