2015-10-09 46 views
1

我不太瞭解死信交換/隊列。在線文檔說:RabbitMQ死信交換/隊列

republished to another exchange when any of the following events occur: 
    The message is rejected (basic.reject or basic.nack) with requeue=false, 
    The TTL for the message expires; or 
    The queue length limit is exceeded. 

這是否意味着當這些事件發生的消息會自動移動到死信隊列?或者我必須專門在我的代碼中將這些「死亡」消息移至該DLQ?

另外我如何設置我的普通隊列的DLX/DLQ?說當我的正常隊列中的消息失敗/過期時,那麼它將轉移到DLX/DLQ?

回答

1

下面是一個完整的例子:

public class DXtest { 
    public static void main(String[] argv) 
      throws IOException, 
      InterruptedException, TimeoutException { 

     ConnectionFactory factory = new ConnectionFactory(); 
     factory.setHost("localhost"); 

     Connection connection = factory.newConnection(); 
     Channel channel = connection.createChannel(); 
     channel.exchangeDeclare("my.exchange", "fanout"); 

     Map<String, Object> args = new HashMap<String, Object>(); 
     args.put("x-dead-letter-exchange", "my.exchange"); 
     args.put("x-max-length", 2); 
     channel.queueDeclare("myqueue", false, false, false, args); // here you setup your queue, 
//for example with x-max-length, when the limit is reach, the message head message queue will be redirect 
//to the my.exchange and then to my-dead-letter-queue 



     channel.queueDeclare("my-dead-letter-queue", false, false, false, null); 
     channel.queueBind("my-dead-letter-queue","my.exchange",""); 


     Consumer consumer = new DefaultConsumer(channel) { 
      @Override 
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) 
        throws IOException { 
       String message = new String(body, "UTF-8"); 
       System.out.println(" [x] Message '" + message + "'" + new Date()); 
      } 
     }; 
     channel.basicConsume("my-dead-letter-queue", true, consumer); 

    } 

} 
相關問題