RabbitMQ + Spring Integration. Queue size 1, only deleted when override

158 Views Asked by At

I would like to now if is posible to implement this idea with RabbitMQ and Spring Integration:

  1. One queue, with capatity for 1 message.
  2. The consumers will ask for this message, if it exist in the queue, it will be delivered to them, if not, they get an null or an Error.
  3. This message ( if exist in the queue ) will not be deleted for had been download, only will be deleted when the producer put another new message in the queue.

Best regards!

1

There are 1 best solutions below

5
On

Something like this:

@Transactional
public Message getMessageFromQueue(String queue) {
    try {
        return this.rabbitTemplate.receive(queue);
    }
    finally {
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    }
}

With the transaction scope we will poll the queue within transaction. With the setRollbackOnly() we rallback TX and, therefore, return the message to the queue back.