问题
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