@JmsListener 上的 ActiveMq 事务 [英] ActiveMq transaction on @JmsListener

查看:44
本文介绍了@JmsListener 上的 ActiveMq 事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试让带有 activeMq 代理的 Jms 消费者获得事务性"确认.我想使用 spring boot 应用程序.

I try to make a Jms consumer with activeMq broker witch have an "transactional" acknowledge. I want to use spring boot application.

我读到我需要 JTA 事务,但我不知道如何开始.

I read that I need JTA transaction but I don't know how I can start one.

我的主要课程:

@SpringBootApplication
@EnableJms
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的消费者:

@Component
public class ReceiveMessage {
    @JmsListener(destination = "${jms.queue.destination}")
    public void receiveMessage(final String msg) throws Exception {
        System.out.println("Received:" + msg);
    }
}

我的 pom.xml 依赖:

My pom.xml dependency :

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
</dependencies>

我必须做什么?

推荐答案

更新您的消费者 :

@Component
public class ReceiveMessage {
    @JmsListener(destination = "${jms.queue.destination}", containerFactory = "jmsListenerContainerFactory")
    public void receiveMessage(final String msg) throws Exception {
        System.out.println("Received:" + msg);
    }
}

添加这些豆子:

@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactory(
        ConnectionFactory connectionFactory,
        DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    factory.setTransactionManager(transactionManager());
    return factory;
}

@Bean
public PlatformTransactionManager transactionManager() {
    JmsTransactionManager transactionManager = new JmsTransactionManager();
    transactionManager.setConnectionFactory(jmsConnectionFactory());
    return transactionManager;
}

@Bean
public QueueConnectionFactory jmsConnectionFactory() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:5672");
    return connectionFactory;
}

这篇关于@JmsListener 上的 ActiveMq 事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆