ActiveMq transaction on @JmsListener

只愿长相守 提交于 2021-02-10 06:38:47

问题


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

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

My main class:

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

My consumer :

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

My pom.xml dependency :

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

What I have to do?


回答1:


update your consumer :

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

add these beans :

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


来源:https://stackoverflow.com/questions/43409193/activemq-transaction-on-jmslistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!