简体   繁体   English

如何在Apache Camel DSL中回滚事务?

[英]how to rollback transaction in Apache Camel DSL?

I wrote this code for education apache camel transaction 我为教育apache骆驼交易编写了此代码

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

I use transacted after an error occurs I have to trigger a transaction rollback, but for some reason the messages remain in the SAMPLE_2 and SAMPLE_3 queues. 发生错误后,我必须使用事务处理,我必须触发事务回滚,但是由于某些原因,消息仍保留在SAMPLE_2和SAMPLE_3队列中。 Why? 为什么?
UPDATE: 更新:
I added bean 我加豆

@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);
}

and fix route but this don't help me: 并修复路线,但这对我没有帮助:

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();

I find in documentation this configuration 我在文档中找到此配置

<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>
...

how to create bean's from this xml? 如何从这个xml创建bean?

Yes this does not work because the Camel JMS component does not know about your transaction manager etc. 是的,这不起作用,因为Camel JMS组件不了解您的事务管理器等。

As you already found out, the component configuration is missing . 如您所知, 缺少组件配置

I don't use plain JMS component but the specialized version for ActiveMQ but they are very similar. 我不使用普通的JMS组件,而是ActiveMQ的专用版本,但是它们非常相似。 So I hope you can derive the missing bean from this one. 因此,我希望您可以从中获得缺少的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;
}

Important 重要

As you see I don't set the transaction manager in the component. 如您所见,我没有在组件中设置事务管理器。 As long as you just talk to 1 broker, you don't need the tx manager stuff (you profit then from cache settings). 只要您只与1个经纪人交谈,就不需要tx经理的东西 (您可以从缓存设置中受益)。 To use transactions with your single broker 与单个经纪人一起使用交易

  • Remove Spring transaction manager 删除Spring交易管理器
  • Remove Spring transaction policy 删除春季交易政策
  • Remove transacted() from Camel route 从骆驼路线中删除transacted()

You just need to set setTransacted(true) and setLazyCreateTransactionManager(false) on the Camel JMS component and then all JMS consumers are transacted. 您只需要在Camel JMS组件上设置setTransacted(true)setLazyCreateTransactionManager(false) ,然后处理所有JMS使用者。

You can also find both setups in the Camel docs . 您也可以在Camel文档中找到这两种设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM