Spring RabbitMQ - 在具有@RabbitListener配置的服务上使用手动通道确认
2022-09-01 12:49:02
如何在不使用自动确认的情况下手动确认消息。有没有办法将其与配置样式一起使用。大多数文档都告诉我们要与 一起使用。但是,使用它,我们失去了注释提供的灵活性。我已按如下方式配置了我的服务:@RabbitListener
@EnableRabbit
SimpleMessageListenerContainer
ChannelAwareMessageListener
@Service
public class EventReceiver {
@Autowired
private MessageSender messageSender;
@RabbitListener(queues = "${eventqueue}")
public void receiveMessage(Order order) throws Exception {
// code for processing order
}
我的兔子配置如下
@EnableRabbit
public class RabbitApplication implements RabbitListenerConfigurer {
public static void main(String[] args) {
SpringApplication.run(RabbitApplication.class, args);
}
@Bean
public MappingJackson2MessageConverter jackson2Converter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
return converter;
@Bean
public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(rabbitConnectionFactory());
factory.setMaxConcurrentConsumers(5);
factory.setMessageConverter((MessageConverter) jackson2Converter());
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return factory;
}
@Bean
public ConnectionFactory rabbitConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost("localhost");
return connectionFactory;
}
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setContainerFactory(myRabbitListenerContainerFactory());
}
@Autowired
private EventReceiver receiver;
}
}
有关如何调整手动通道确认以及上述配置样式的任何帮助,我们将不胜感激。如果我们实现 ChannelAwareMessageListener,则 onMessage 签名将会更改。我们可以在服务上实现 ChannelAwareMessageListener 吗?