package com.ankurm.rabbit; import com.rabbitmq.client.GetResponse; import org.junit.jupiter.api.Test; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.QueueBuilder; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; /** * The third dead-letter trigger: a queue that is full. * *
Included because the article states all three triggers in one table, and this was the only * row not produced by a run. It is now. * * @see docs/04-dead-lettering.md */ @SpringBootTest class MaxLengthTest { static final String Q_BOUNDED = "orders.bounded"; @Autowired RabbitTemplate template; @Autowired RabbitAdmin admin; @Test @SuppressWarnings("unchecked") void exceedingMaxLengthDeadLettersTheOldestWithReasonMaxlen() throws Exception { this.admin.deleteQueue(Q_BOUNDED); Queue bounded = QueueBuilder.durable(Q_BOUNDED) .maxLength(2) .deadLetterExchange(Topology.DLX) .deadLetterRoutingKey("failed") .build(); this.admin.declareQueue(bounded); Binding binding = BindingBuilder.bind(bounded) .to(new DirectExchange(Topology.DIRECT)).with("bounded"); this.admin.declareBinding(binding); this.admin.purgeQueue(Topology.Q_DLQ, false); // Three messages into a queue that holds two. RabbitMQ drops from the HEAD, so the // FIRST message is the one dead-lettered - the oldest, not the newest. for (String id : List.of("m-1", "m-2", "m-3")) { this.template.convertAndSend(Topology.DIRECT, "bounded", OrderMessage.of(id)); } var message = this.template.receive(Topology.Q_DLQ, 10_000); assertThat(message).isNotNull(); List