Rabbitmq wait after processing message

I have following rabbit listener:

@Slf4j
@Service
@RequiredArgsConstructor
@RabbitListener(queues = "${spring.rabbitmq.template.default-receive-queue}")
public class RabbitmqListener {
    private final Processor processor;

    @RabbitHandler(isDefault = true)
    public void receiveMessage(List<String> someData) {
        log.info("Received {} some data", someData.size());
        processor.process(someData);
        //should wait for 15 minutes here
    }
}

I need to configure listener to wait for 15 minutes after it processed one message before receiving next one. Not necessary to wait inside this method. All i need is NOT to receive any messages after processing one. It could be done by Thread.sleep(15000), but i’m not sure that it’s the best way to achieve this. Is there any rabbitmq configuration for this kind of situation?

Answer

Thread.sleep(15000) will wait for 15 seconds, not 15 minutes.

It’s probably not a good idea to sleep for 15 minutes; if you only need to sleep for 15 seconds, a sleep is probably ok (but you do risk a redelivery if the server crashes while you are are sleeping).

You might want to consider using RabbitTemplate.receiveAndConvert() instead for this use case, rather than using a message-driven architecture.

Leave a Reply

Your email address will not be published. Required fields are marked *