概念
Rollback EIP 用于将Exchange标记为回滚并停止继续路由消息。
示例
回滚异常
在 DSL 中使用rollback 标签标记,rollback 内部会抛出一个 RollbackExchangeException 异常,并且会停止继续执行路由消息,如下示例,rollback 之后将不再执行 log 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class RollbackTest extends ContextTestSupport {
@Test public void testRollback() throws Exception { getMockEndpoint("mock:result").expectedMessageCount(0); getMockEndpoint("mock:rollback").expectedMessageCount(1);
try { template.requestBody("direct:start", "bad"); fail("Should have thrown a RollbackExchangeException"); } catch (RuntimeCamelException e) { boolean b = e.getCause() instanceof RollbackExchangeException; assertTrue(b); }
assertMockEndpointsSatisfied(); }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").choice().when(body().isNotEqualTo("ok")).to("mock:rollback") .rollback("That do not work") .log("hello") .otherwise().to("mock:result").end(); } }; } }
|
回滚标记
使用 markRollbackOnly 方法可以将此行为修改为仅标记回滚,而不抛出异常。
1 2 3 4 5
| from("direct:start") .choice().when(body().contains("error")) .markRollbackOnly() .otherwise() .to("direct:continue");
|