2014-09-12 146 views
3

我嘗試構建持續消息隊列,每個消息都有一些延遲。在Java代碼它看起來像這樣:RabbitMQ中的消息丟失

ConnectionFactory factory = new ConnectionFactory(); 
    factory.setHost("localhost"); 
    Connection connection = factory.newConnection(); 
    Channel channel = connection.createChannel(); 

    channel.exchangeDeclare("WorkExchange", "direct"); 
    channel.queueDeclare("WorkQueue", true, false, false, null); 
    channel.queueBind("WorkQueue", "WorkExchange", ""); 

    Map<String, Object> args = new HashMap<>(); 
    args.put("x-dead-letter-exchange", "WorkExchange"); 

    channel.exchangeDeclare("RetryExchange", "direct"); 
    channel.queueDeclare("RetryQueue", true, false, false, args); 
    channel.queueBind("RetryQueue", "RetryExchange", ""); 

    channel.confirmSelect(); 
    BasicProperties properties = new BasicProperties(); 
    properties.setDeliveryMode(2); 
    properties.setExpiration("120000"); 
    channel.basicPublish("RetryExchange", "", properties, "Hello world!".getBytes()); 
    channel.waitForConfirmsOrDie(); 
    connection.close(); 

但是,我有一些持久性的問題。當我停止服務器時,等待一段時間並重新啓動它,必須移動到WorkQueue的消息纔會消失。我做錯了什麼?或者它是由設計?

回答

6

但是,我有一些持久性問題。當我停止服務器時,請等待一段時間並重新啓動, 必須移至WorkQueue的消息纔會消失。我做錯了什麼?或者它是由設計?

您應該使用MessageProperties來使您的消息持久。

channel.basicPublish("", "task_queue", 
     MessageProperties.PERSISTENT_TEXT_PLAIN, 
     message.getBytes()); 

您當前的代碼`channel.queueDeclare( 「RetryQueue」 ,假的,假的,參數);將使隊列持久化,但不是消息。

更多這裏RabbitMQ Doc