Question

I use spring amqp with rabbitmq. I want get one message without prefetch. I configured with

      SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(rabbitConnectionFactory());
    container.setQueueNames(
            ProjectConfigs.getInstance().get_RABBIT_TASK_QUEUE()
    );
    container.setMessageListener(taskListener());
    container.setConcurrentConsumers(1);
    container.setPrefetchCount(1);
    container.setTxSize(1);
    return container;

How to disable prefetch and get only one message/

Was it helpful?

Solution

prefetch simply controls how many messsages the broker allows to be outstanding at the consumer at a time. When set to 1, this means the broker will send 1 message, wait for the ack, then send the next.

It defaults to 1. Setting it to 0 will mean the broker will send unlimited messages to the consumer, regardless of acks.

If you only want one message and then stop, you shouldn't use a container, you can use one of the RabbitTemplate.receive() methods.

OTHER TIPS

I try do it with Spring AMQP

@Bean
public MessageListener taskListener() {
    return new MessageListener() {
        public void onMessage(Message message) {
            try {
                LOGGER.info(new String(message.getBody(), "UTF-8"));
                Converter converter = new Converter();
                converter.startConvert(new String(message.getBody(), "UTF-8"));
            } catch (Exception e) {
                LOGGER.error(getStackTrace(e));
            }
        }
    };
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top