我试图让一个拥有activeMq经纪人的Jms消费者拥有"事务性"确认。我想使用spring boot应用程序。
我读到我需要JTA交易,但我不知道如何开始交易。
我的主要课程:
@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依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
我必须做什么?
答案 0 :(得分:1)
更新您的消费者:
@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:
@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;
}