[英]spring rabbit and spring transactions
I have a spring rabbit consumer like: 我有一个春季兔子消费者,例如:
@Override public void onMessage(Message amqpMessage, Channel channel)
throws Exception {
//..some code goes here - I want it to be in spring transaction
}
The issue is the code which is in onMessage method is not under transaction. 问题是onMessage方法中的代码不在事务中。 I checked it, I save data to 3 tables, then throw exception, then save to 4th table.
我检查了一下,将数据保存到3个表,然后抛出异常,然后保存到第4个表。 And data from 3 previos tables is not being rolled back.
并且3个previos表中的数据不会回滚。 How to do that properly in spring?
春天该怎么做呢? I want all code in onMessage method to be within a transaction.
我希望onMessage方法中的所有代码都在事务内。 Thanks
谢谢
UPDATE My rabbit conf: 更新我的兔子conf:
@Configuration @ComponentScan(basePackages = {"com.mycompany"})
public class TicketModeRabbit {
@Bean TicketModeConsumer ticketModeConsumer() {
return new TicketModeConsumer();
}
@Bean(name = TicketModeRabbitData.QUEUE_BEAN_NAME) Queue queue() {
return new Queue(TicketModeRabbitData.QUEUE_BEAN_NAME);
}
@Bean(name = TicketModeRabbitData.QUEUE_BINDING_NAME) Binding binding(
@Qualifier(TicketModeRabbitData.QUEUE_BEAN_NAME) Queue q, TopicExchange e) {
return BindingBuilder.bind(q).to(e).with(TicketModeRabbitData.QUEUE_TOKEN_NAME);
}
@Bean(name = TicketModeRabbitData.CONTAINER_NAME)
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
@Qualifier(TicketModeRabbitData.LISTENER_ADAPTED_NAME)
MessageListenerAdapter listenerAdapter) {
return WorkerConfigHelper
.rabbitConfigListenerContainer(connectionFactory, listenerAdapter,
TicketModeRabbitData.QUEUE_BEAN_NAME,
WorkerConfigHelper.GLOBAL_CONCURRENT_CONSUMERS);
}
@Bean(name = TicketModeRabbitData.LISTENER_ADAPTED_NAME)
MessageListenerAdapter listenerAdapter() {
return new MessageListenerAdapter(ticketModeConsumer());
}
}
If your transaction manager is properly set up for your database, the only thing you need to do is add the @Transactiona
l annotation on the onMessage
method. 如果为数据库正确设置了事务管理器,则唯一要做的就是在
onMessage
方法上添加@Transactiona
l批注。 Note that the consumer ( MessageListener
) needs to be a bean managed by the Spring
container. 请注意,使用者(
MessageListener
)必须是由Spring
容器管理的bean。
@Override
@Transactional
public void onMessage(Message amqpMessage, Channel channel)
throws Exception {
//..some code goes here - I want it to be in spring transaction
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.