本文介绍了Spring RabbitMQ - 在具有@RabbitListener配置的服务上使用手动通道确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用自动确认的情况下手动确认消息。
是否可以使用此方法以及 @RabbitListener @EnableRabbit 配置样式。
大多数文档告诉我们使用 SimpleMessageListenerContainer 以及 ChannelAwareMessageListener
但是使用它会失去注释提供的灵活性。
我已按以下方式配置我的服务:

How to acknowledge the messages manually without using auto acknowledgement.Is there a way to use this along with the @RabbitListener and @EnableRabbit style of configuration.Most of the documentation tells us to use SimpleMessageListenerContainer along with ChannelAwareMessageListener.However using that we lose the flexibility that is provided with the annotations.I have configured my service as below :

@Service
public class EventReceiver {

@Autowired
private MessageSender messageSender;

@RabbitListener(queues = "${eventqueue}")
public void receiveMessage(Order order) throws Exception {

  // code for processing order
}






我的RabbitConfiguration如下




My RabbitConfiguration is as below

@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吗?

Any help will be appreciated on how to adapt manual channel acknowledgement along with the above style of configuration.If we implement the ChannelAwareMessageListener then the onMessage signature will change.Can we implement ChannelAwareMessageListener on a service ?

推荐答案

添加频道 @RabbitListener 方法......

Add the Channel to the @RabbitListener method...

@RabbitListener(queues = "${eventqueue}")
public void receiveMessage(Order order, Channel channel,
    @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {
    ...
}

并使用 basicAck basicReject

编辑

@SpringBootApplication
@EnableRabbit
public class So38728668Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So38728668Application.class, args);
        context.getBean(RabbitTemplate.class).convertAndSend("", "so38728668", "foo");
        context.getBean(Listener.class).latch.await(60, TimeUnit.SECONDS);
        context.close();
    }

    @Bean
    public Queue so38728668() {
        return new Queue("so38728668");
    }

    @Bean
    public Listener listener() {
        return new Listener();
    }

    public static class Listener {

        private final CountDownLatch latch = new CountDownLatch(1);

        @RabbitListener(queues = "so38728668")
        public void receive(String payload, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag)
                throws IOException {
            System.out.println(payload);
            channel.basicAck(tag, false);
            latch.countDown();
        }

    }

}

application.properties:

application.properties:

spring.rabbitmq.listener.acknowledge-mode=manual

这篇关于Spring RabbitMQ - 在具有@RabbitListener配置的服务上使用手动通道确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:51