点击按钮从RabbitMQ获取消息

时间:2017-11-29 09:58:19

标签: c# rabbitmq

我正在尝试在按钮点击时使用RabbitMq队列中的消息。 当我把这个代码放在一个控制台应用程序时它可以工作,但当我使用按钮点击事件时,它将无法正常工作。 我希望你能帮助我。

这是服务器代码:

public void PushToQueue(Message message)
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "task_queue",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            string messageToSend = JsonConvert.SerializeObject(message);
            var body = Encoding.UTF8.GetBytes(messageToSend);

            //var properties = channel.CreateBasicProperties();
            //properties.Persistent = true;

            channel.BasicPublish(exchange: "",
                                 routingKey: "task_queue",
                                 basicProperties: null,
                                 body: body);
            Debug.WriteLine(" [x] Verzonden {0}", messageToSend);
        }
    }

这是客户端代码:

public void GetFromQueue()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "task_queue",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Debug.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: "task_queue",
                                 autoAck: true,
                                 consumer: consumer);
        }
    }

这是te按钮点击代码:

private void buttonGetFromQueue_Click(object sender, EventArgs e)
    {
        rabbitManager.GetFromQueue();
    }

我走过了代码,看来它不会到达这个部分:

var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Debug.WriteLine(" [x] Received {0}", message);

这是兔子经理班:

public class RabbitMqManager
{
    public List<Message> allMessages = new List<Message>();
    public RabbitMqManager()
    {

    }

    public void GetFromQueue()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "task_queue",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Debug.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: "task_queue",
                                 autoAck: true,
                                 consumer: consumer);
        }
    }

    public List<Message> ReturnAllMessages()
    {
        List<Message> returnList = new List<Message>();

        foreach(Message m in allMessages)
        {
            returnList.Add(m);
        }
        return returnList;
    }
}

消息被推送到队列但我无法将它们排除在外。

1 个答案:

答案 0 :(得分:0)

要从RabbitMQ检索消息,我们需要首先连接到RabbitMQ服务器:

public WebClient GetRabbitMqConnection(string userName, string password)
{
    var client = new WebClient(); 
    client.Credentials = new NetworkCredential(userName, password);
    return client;
}

现在使用以下命令从Rab​​bitMQ检索消息:

public string GetRabbitMQMessages(string domainName, string port, string 
            queueName, string virtualHost, WebClient client, string 
            methodType)
{
    string messageResult = string.Empty;
    string strUri = "http://" + domainName + ":" + port + 
                    "/api/queues/" + virtualHost + "/";
    var data = client.DownloadString(strUri + queueName + "/");
    var queueInfo = JsonConvert.DeserializeObject<QueueInfo>(data);
    if (queueInfo == null || queueInfo.messages == 0)
               return string.Empty;
    if (methodType == "POST")
    {
        string postbody = "  
        {\"ackmode\":\"ack_requeue_true\",\"count\":
         \"$totalMessageCount\",\"name\":\"${DomainName}\",
         \"requeue\":\"false\",\"encoding\":\"auto\",\"vhost\" :
         \"${QueueName}\"}";
         postbody = postbody.Replace("$totalMessageCount", 
         queueInfo.messages.ToString()).Replace("${DomainName}", 
         domainName).Replace("${QueueName}", queueName);
         messageResult = client.UploadString(strUri + queueName + 
         "/get", "POST", postbody);
  }
  return messageResult;

}

我认为这将帮助您实现RabbitMQ。