以下のプログラムは基本的にC#Rabbit MQチュートリアルのReceiver/Workerプログラムのプログラムです:https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html(カウンタが追加されています)。RabbitMQ BasicConsumeとConsole.ReadLine()に関するイベントドリブンの問題
1)私は「Console.ReadLine()」をコメントアウトした場合、それは消費キューからのメッセージと表示されます:
私はそれについて困惑している二、三のものがあります。
Start Press [enter] to exit. My End - CountMessagesProcessed=0
私がテストした最初の数回は、何が起こっていたのか理解できませんでした。
2)この行は決して出力に表示されません。Console.WriteLine( "[Enter]を押して終了します。");おそらくそれは "Console.ReadLine();"の前ですから、なぜですか? ReadLineイベントとBasicConsumerの相互作用は何ですか?
3)MQチュートリアルのページには、「リスナー」プロセスを停止するためにCNTL-Cを使用すると記載されていますが、Enterキーを押すだけで同等の結果が得られます。
以前はMQSeriesのリスナーを作成しましたが、これはスレッディングが好きかもしれませんが、提供される基本的なチュートリアルを理解しようとしています。
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);
}
}
}
}