Dead letter queue DLX (dead-letter-exchange)

DLX use, when the message becomes a dead letter (dead message) in a queue, it can be re-publish to another Exchange, the Exchange is DLX. There has been turned into dead-letter message about several situations:

  • Message is rejected (basic.reject / basic.nack) and requeue = false
  • TTL expired messages
  • The maximum length of the queue

DLX is a normal Exchange, the Exchange and the general is no different, it can be specified on any queue, a queue is actually a set of attributes, when the ranks were filled with dead letter, RabbitMQ will automatically bring this Exchange message to republish set up, and then be routed to another queue, the queue can listen to the message corresponding processing.

Dead letter queue settings:

exchange: dlx.exchange

queue: dlx.queue

routingkey:  #

Then switch the normal statement, queues, binding, but need to add a parameter to the queue: argument.put ( "x-dead-letter-exchange", "dlx.exchange":

public static void createQueue(){
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(ip);
        factory.setPort(port);
        factory.setUsername(username);
        factory.setPassword(password);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        Map<String, Object>  argss = new HashMap<String, Object>();
        argss.put("vhost", "/");
        argss.put("username","root");
        argss.put("password", "root");
        argss.put("x-message-ttl",6000);
        argss.put("x-dead-letter-exchange","exchange.dlx.test");
 		argss.put("x-dead-letter-routing-key","queue.dlx.test");
        channel.queueDeclare("queue.dlx.test", durable, exclusive, autoDelete, argss);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
}

Such message expires, requeue, when the maximum length of the queue, the message can be routed directly to the dead letter queue.

Published 273 original articles · won praise 13 · views 70000 +

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105136873