74 lines
2.6 KiB
Java
74 lines
2.6 KiB
Java
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.
|
|
*
|
|
* <p>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 <a href="../../../../../docs/04-dead-lettering.md">docs/04-dead-lettering.md</a>
|
|
*/
|
|
@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<Map<String, Object>> deaths =
|
|
(List<Map<String, Object>>) message.getMessageProperties().getHeader("x-death");
|
|
System.out.println("=== x-death after x-max-length overflow ===");
|
|
deaths.get(0).forEach((k, v) -> System.out.printf(" %-16s %s%n", k, v));
|
|
System.out.println(" body " + new String(message.getBody()));
|
|
|
|
assertThat(deaths.get(0)).containsEntry("reason", "maxlen");
|
|
assertThat(deaths.get(0)).containsEntry("queue", Q_BOUNDED);
|
|
assertThat(new String(message.getBody())).contains("m-1");
|
|
}
|
|
|
|
}
|