如果队列为空,则停止Rabbit MQ使用者事件

时间:2017-11-08 13:49:37

标签: .net rabbitmq

我在RabbitMQ中添加了EventingBasicConsumer的Received Event Handler。我试图检查队列是否被消耗(处理并且现在为空),它应该关闭消费者和连接。我无法找到可以判断队列是否被处理的条件。

请帮忙

@Test
public void parseInputValueFromHtml() {
    String html = "<input type = \"text\" id = \"national\" value = \"3.26\" style = \"width: 2.3em;\">";

    Document document = Jsoup.parse(html);

    Element mdolar = document.getElementById("national");
    Assert.assertEquals("3.26", mdolar.attr("value"));

    // you can also find this element by type:
    Elements mdolars = document.select("input[id=national]");
    Assert.assertEquals(1, mdolars.size());
    Assert.assertEquals("3.26", mdolars.first().attr("value"));
}

3 个答案:

答案 0 :(得分:0)

我无法找到任何属性,因此必须实现一个定时器,每次收到消息时都会重置,如果经过的时间超过2分钟,你可以触发一个清理方法,这将停止消费者并关闭连接

答案 1 :(得分:0)

由于我没有找到任何方法来阻止Rabbit MQ使用者事件,如果队列是空的,我已经实现了以下方法来处理消息,方法是从API传递消息计数

&#34;本地主机:/ API /队列&#34;

以下是处理消息直到队列为空的函数

/// <summary>
/// (Recommanded) Processes the queue till the number of messages provided.
/// Added to manage the load (process batches by batches)
/// </summary>
/// <param name="queueName">Name of the queue.</param>
/// <param name="ProcessMessage">The process message.</param>
/// <param name="count">The count.</param>
public uint ProcessQueueByMessageCount(string queueName, Func<string, bool> HowToProcessMessage, uint messageCount)
{
    uint messagesToProcess = messageCount;
    using (var connect = this)
    {
        while (messageCount > 0)
        {
            BasicGetResult result = connect.Model.BasicGet(queueName, false);
            bool processed = HowToProcessMessage.Invoke(System.Text.Encoding.UTF8.GetString(result.Body));
            if (processed)
            {
                this.SendAcknowledgement(result.DeliveryTag);
                messageCount--;
            }
            else
            {
                connect.Model.BasicNack(result.DeliveryTag, false, true);
                break;
            }
        }
    }
    return messagesToProcess - messageCount;
}

答案 2 :(得分:0)

我只是创建一个被动队列来窥视队列中有多少消息

 private static int passiveDeclareForMessageCount(IModel model)
    {

        Dictionary<string, object> args = new Dictionary<string, object>();
        args.Add("x-queue-mode", "lazy");
        int resultCount = 0;
        var response = model.QueueDeclarePassive(ConfigurationManager.AppSettings["LocalQueueName"].ToString());
        resultCount = (int)response.MessageCount;

        return resultCount;
    }