【问题标题】:Apache ActiveMQ Camel transacted rollbackApache ActiveMQ Camel 事务回滚
【发布时间】:2013-09-19 11:04:15
【问题描述】:

为了更好地理解 ActiveMQ 和 Camel,我正在编写一个用于事务回滚的单元测试。它似乎对我不起作用,这意味着我做错了什么!代码如下:

public class MyTest extends CamelTestSupport {
@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry reg = super.createRegistry();

    DataSourceTransactionManager txMgr = new DataSourceTransactionManager();

    SpringTransactionPolicy txPolicy = new SpringTransactionPolicy();
    txPolicy.setTransactionManager(txMgr);
    txPolicy.setPropagationBehaviorName("PROPAGATION_REQUIRED");
    reg.bind("required", txPolicy);

    return reg;
}

@Before
public void setUp() throws Exception {
    super.setUp();
}

@After
public void tearDown() throws Exception {
    super.tearDown();
}

@Test
public void testTransaction() throws Exception {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    context.addComponent("jms", JmsComponent.jmsComponentTransacted(connectionFactory));
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("jms:queue:in")
                    .transacted("required")
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("Expected failure");
                            throw new RuntimeException("Expected failure");
                        }
                    })
                    .to("mock:result");
        }
    });
    context.start();

    MockEndpoint result = context.getEndpoint("mock:result", MockEndpoint.class);
    result.expectedMessageCount(0);

    NotifyBuilder notifyBuilder = new NotifyBuilder(context).whenDone(1).create();
    context.createProducerTemplate().sendBody("jms:queue:in", "Test");

    boolean matches = notifyBuilder.matches(5, TimeUnit.SECONDS);
    assertTrue(matches);

    Thread.sleep(1000);
    assertMockEndpointsSatisfied();

    BrowsableEndpoint in = context.getEndpoint("jms:queue:in", BrowsableEndpoint.class);
    List<Exchange> list = in.getExchanges();
    assertEquals(1, list.size());
    String body = list.get(0).getIn().getBody(String.class);
    assertEquals("Test", body);

    context.stop();
}

}

它在 list.size() 为 1 的断言上失败,如果回滚成功,它应该通过。我究竟做错了什么?提前致谢!

【问题讨论】:

    标签: java transactions activemq apache-camel


    【解决方案1】:

    在您的情况下,您使用的是DataSourceTransactionManager,而您需要的是引用您的ConnectionFactoryorg.springframework.jms.connection.JmsTransactionManager。也可以将?transacted=true 添加到 JMS URI 中,而根本不引用事务管理器 - 这使用本地 JMS 事务。

    在这两种情况下,默认情况下,消息将在 ActiveMQ 中的死信队列中结束,而不是返回到原始队列中。这种行为可以是configured

    【讨论】:

      猜你喜欢
      • 2015-12-31
      • 2019-12-31
      • 2020-11-11
      • 1970-01-01
      • 2021-12-09
      • 2023-03-06
      • 2021-03-07
      • 2014-05-23
      • 2017-02-04
      相关资源
      最近更新 更多