2015-11-17 51 views
0

我已經使用RabbitMQ存儲信息。我注意到應用程序重新啓動時消息被刪除。信息已自動刪除RabbitMQ

我在同一個應用程序中有生產者和消費者。

請找到生產者和消費者如下。我使用了持久隊列以及持久消息。

因此,如果只有一個隊列使用者,並且當前沒有使用,則會刪除隊列消息。是這樣嗎 ?

監製:

public static void PublishMessage(RequestDto message, string queueName) 
    { 
     var factory = new ConnectionFactory() { HostName = Config.RabbitMqHostName, Port = Config.RabbitMqPortNumber }; 

     using (var connection = factory.CreateConnection()) 
     { 
      using (var channel = connection.CreateModel()) 
      { 
       channel.QueueDeclare(queueName, true, false, false, null); 

       var properties = channel.CreateBasicProperties(); 
       properties.SetPersistent(true); 
       // properties.DeliveryMode = 2; I have used this too. 

       string serializesMessage = Utility.SerializeSoapObject(message); 

       var messageBytes = Encoding.UTF8.GetBytes(serializesMessage); 

       channel.BasicPublish("", queueName, properties , messageBytes); 

       Log.Info("Record added into queue : \nMessage: " + serializesMessage); 
      } 
     } 
    } 

消費者:

var factory = new ConnectionFactory() { HostName = Config.RabbitMqHostName, Port = Config.RabbitMqPortNumber }; 
      using (var connection = factory.CreateConnection()) 
      { 
       using (var channel = connection.CreateModel()) 
       { 
        channel.QueueDeclare(Config.RabbitMqQueueName, true, false, false, null); 

        var consumer = new QueueingBasicConsumer(channel); 
        channel.BasicConsume(Config.RabbitMqQueueName, true, consumer); 

        while (DoProcessMessage()) 
        { 
          try 
          { 
           List<RequestDto> messages = GetMessagesInBatch(consumer); 

           if (messages.Count > 0) 
           { 
            ProcessMessageInParallel(messages); 
           } 
           else 
           { 
            Producer.FillRequestMessages(); 
           } 
          } 
          catch (Exception exception) 
          { 
           Log.Error("StartConsumer - Failed to process message from RabbitMq Error: " + exception.Message, exception); 
          } 

        } 
       } 
      } 
     } 
     catch (Exception exception) 
     { 
      Log.Error(exception.Message, exception); 
     } 

    private bool DoProcessMessage() 
    { 
     return Config.MaxRequestPerDayCount > 1000; 
    } 

如果任何人都可以提供幫助。

+0

您可以通過RabbitMQ管理面板確認該隊列是否持久,並且其中的消息具有delivery_mode = 2?是否有更改任何邏輯,刪除或清除應用程序關閉或啓動隊列? – pinepain

回答