【问题标题】:how to rollback transaction in Apache Camel DSL?如何在 Apache Camel DSL 中回滚事务?
【发布时间】:2019-12-31 21:18:29
【问题描述】:

我为教育 apache camel 交易编写了这段代码

    from("jms:SAMPLE_1")
            .transacted()
            .log("message")
            .to("jms:SAMPLE_2")
            .to("jms:SAMPLE_3")
            .log("message")
            .process(exchange -> {
                throw new Exception();
            })
           .end();

我在发生错误后使用事务处理,我必须触发事务回滚,但由于某种原因,消息仍保留在 SAMPLE_2 和 SAMPLE_3 队列中。为什么?
更新:
我加了豆子

@Bean(name = "PROPAGATION_REQUIRED")
public SpringTransactionPolicy propogationRequired(PlatformTransactionManager jtaTransactionManager){
    SpringTransactionPolicy propagationRequired = new SpringTransactionPolicy();
    propagationRequired.setTransactionManager(jtaTransactionManager);
propagationRequired.setPropagationBehaviorName("PROPAGATION_REQUIRED");
    return propagationRequired;
}
@Bean
PlatformTransactionManager platformTransactionManager(ConnectionFactory cf) {
    return new JmsTransactionManager(cf);
}

并修复路线,但这对我没有帮助:

from("jms:SAMPLE_1")
            .transacted("PROPAGATION_REQUIRED")
            .log("message")
            .to("jms:SAMPLE_2")
            .to("jms:SAMPLE_3")
            .log("message")
            .process(exchange -> {
                throw new Exception();
            })
           .end();

我在文档中找到了这个配置

<blueprint ...>
<bean id="jmstx" class="org.apache.camel.component.jms.JmsComponent">
    <property name="configuration" ref="jmsConfig" />
</bean>

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="jmsConnectionFactory" />
    <property name="transactionManager" ref="jmsTransactionManager" />
    <property name="transacted" value="true" />
</bean>
...

如何从这个 xml 创建 bean?

【问题讨论】:

    标签: java transactions apache-camel spring-transactions


    【解决方案1】:

    是的,这不起作用,因为 Camel JMS 组件不知道您的事务管理器等。

    您已经发现,缺少组件配置

    我不使用普通的 JMS 组件,而是使用 ActiveMQ 的专用版本,但它们非常相似。所以我希望你能从这个中推导出缺失的 bean。

    @Bean(name = "activemq") // bean name is used in URI activemq:...
    public ActiveMQComponent createComponent(ConnectionFactory factory) {
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConnectionFactory(factory);
        activeMQComponent.setLazyCreateTransactionManager(false);
        activeMQComponent.setTransacted(true);
        return activeMQComponent;
    }
    

    重要

    如您所见,我没有在组件中设置事务管理器。只要您只与 1 个经纪人交谈,您就不需要 tx 管理器的东西(然后您可以从缓存设置中获利)。与您的单一经纪人一起使用交易

    • 删除 Spring 事务管理器
    • 删除 Spring 事务策略
    • 从骆驼路线中删除transacted()

    您只需要在 Camel JMS 组件上设置 setTransacted(true)setLazyCreateTransactionManager(false),然后所有 JMS 消费者都会被处理。

    您还可以找到这两种设置in the Camel docs

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 2015-12-31
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多