与Console.ReadLine()有关的RabbitMQ BasicConsume和事件驱动问题 [英] RabbitMQ BasicConsume and Event Driven Issues relating to Console.ReadLine()

查看:402
本文介绍了与Console.ReadLine()有关的RabbitMQ BasicConsume和事件驱动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的程序基本上是C#Rabbit MQ教程中Receiver/Worker程序的程序: https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html (已添加计数器).

The program below is basically the program from Receiver/Worker program from the C# Rabbit MQ Tutorial here: https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html (with a counter added).

有两三件事让我感到困惑:

There are two or three things that have me stumped about it:

1)如果我注释掉"Console.ReadLine()",它将使用队列中的消息并显示:

1) If I comment out the "Console.ReadLine()" it consumes the messages from the Queue and displays:

Start 
Press [enter] to exit. 
My End - CountMessagesProcessed=0

最初的几次测试中,我不知道发生了什么.

The first few times I was testing, I couldn't figure out what was going on.

2)此行永远不会显示在输出中:Console.WriteLine(按[enter]退出.");.大概是因为它在"Console.ReadLine();"之前,但是为什么呢? ReadLine事件和BasicConsumer之间有什么相互作用?

2) This line never shows up in the output: Console.WriteLine(" Press [enter] to exit.");. Presumably because it's before the "Console.ReadLine();", but why? What is the interplay between the ReadLine event and the BasicConsumer?

3)MQ教程页面说使用CNTL-C来停止侦听器"过程,但是我发现仅按Enter键同样有效.

3) The MQ Tutorial page says to use CNTL-C to stop the "listener" process, but I find that just pressing enter works equally well.

我以前用线程编写了MQSeries的侦听器,我可能会更喜欢它,但是只是想了解提供的基本教程.

I've written listeners for MQSeries before, with threading, which I might like better, but just trying to understand the basic tutorials provided.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace RabbitMQReceiver
{
    class Receive
    {
        public static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            var myQueuename = "MyQueueName1";
            Console.WriteLine("My Start");


            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(queue: myQueuename,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                var consumer = new EventingBasicConsumer(channel);
                int countMessagesProcessed = 0;

                // this chunk of code is passed as parm/variable to BasicConsume Method below to process each item pulled of the Queue 
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body);
                    countMessagesProcessed++;
                    Console.WriteLine(" [x] Received {0}", message);
                }

                channel.BasicConsume(queue: myQueuename,
                                     noAck: true,
                                     consumer: consumer);

                Console.WriteLine(" Press [enter] to exit.");  // this line never shows up in output 
                Console.ReadLine();    // if this line is commented out the message are consumed, but no Console.WriteLines appear at all. 
                Console.WriteLine("My End - CountMessagesProcessed=" + countMessagesProcessed);

            }
        }
    }
}

推荐答案

Console.ReadLine()在等待输入时停止此时的程序执行,这允许RabbitMQ在此期间使用其运行的线程.已注释掉,程序执行一直运行到最后并退出,包括RabbitMQ线程.

Console.ReadLine() halts execution of your program at that point while waiting for input, which allows the threads RabbitMQ is using to run in the meantime. Commented out, program execution runs to the end and exits, including the RabbitMQ threads.

是的,您可以键入任何内容,这将终止程序的执行;按下键后,程序将继续执行并运行到最后.

Yes, you can type anything and it will halt execution of the program; once you hit the key, program execution will continue and run to the end.

这篇关于与Console.ReadLine()有关的RabbitMQ BasicConsume和事件驱动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆