ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Apache ActiveMQ Camel事务回滚

2019-06-29 09:48:30  阅读:206  来源: 互联网

标签:java activemq transactions apache-camel


为了更好地理解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的断言上失败,如果回滚成功则应该通过该断言.我究竟做错了什么?提前致谢!

解决方法:

在您的情况下,当您需要的是引用您的ConnectionFactory的org.springframework.jms.connection.JmsTransactionManager时,您正在使用DataSourceTransactionManager.也可以将?transacted = true添加到JMS URI而不是引用事务管理器 – 这使用本地JMS事务.

在这两种情况下,默认情况下会发生的消息是,消息将在ActiveMQ中的死信队列中结束,而不是在原始队列中.此行为可能是configured.

标签:java,activemq,transactions,apache-camel
来源: https://codeday.me/bug/20190629/1325407.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有