以下是C# RabbitMQ简单收发消息的示例代码的完整攻略,包含两个示例说明。
示例1:简单队列模式
步骤1:安装RabbitMQ
首先,您需要安装RabbitMQ。您可以从RabbitMQ官网下载适合您操作系统的安装包进行安装。
步骤2:添加依赖
在Visual Studio中,您需要使用NuGet包管理器添加以下依赖:
- RabbitMQ.Client
步骤3:发送消息
using RabbitMQ.Client;
using System;
using System.Text;
class Producer
{
static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "my_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: "my_queue", basicProperties: null, body: body);
Console.WriteLine("Message sent: {0}", message);
}
}
}
步骤4:接收消息
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
class Consumer
{
static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "my_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Message received: {0}", message);
};
channel.BasicConsume(queue: "my_queue", autoAck: true, consumer: consumer);
Console.WriteLine("Waiting for messages...");
Console.ReadLine();
}
}
}
步骤5:运行程序
运行发送消息的程序后,您将看到以下输出:
Message sent: Hello World!
运行接收消息的程序后,您将看到以下输出:
Waiting for messages...
Message received: Hello World!
示例2:工作队列模式
步骤1:安装RabbitMQ
同示例1。
步骤2:添加依赖
同示例1。
步骤3:发送消息
using RabbitMQ.Client;
using System;
using System.Text;
class Producer
{
static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "my_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
for (int i = 1; i <= 10; i++)
{
string message = "Message " + i;
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: "my_queue", basicProperties: null, body: body);
Console.WriteLine("Message sent: {0}", message);
}
}
}
}
步骤4:接收消息
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading;
class Consumer
{
static void Main(string[] args)
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "my_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Message received: {0}", message);
Thread.Sleep(5000);
Console.WriteLine("Message processed");
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "my_queue", autoAck: false, consumer: consumer);
Console.WriteLine("Waiting for messages...");
Console.ReadLine();
}
}
}
步骤5:运行程序
运行发送消息的程序后,您将看到以下输出:
Message sent: Message 1
Message sent: Message 2
Message sent: Message 3
Message sent: Message 4
Message sent: Message 5
Message sent: Message 6
Message sent: Message 7
Message sent: Message 8
Message sent: Message 9
Message sent: Message 10
运行接收消息的程序后,您将看到以下输出:
Waiting for messages...
Message received: Message 1
Message processed
Message received: Message 2
Message processed
Message received: Message 3
Message processed
Message received: Message 4
Message processed
Message received: Message 5
Message processed
Message received: Message 6
Message processed
Message received: Message 7
Message processed
Message received: Message 8
Message processed
Message received: Message 9
Message processed
Message received: Message 10
Message processed
以上就是C# RabbitMQ简单收发消息的示例代码的完整攻略,包含两个示例说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:c# rabbitmq 简单收发消息的示例代码 - Python技术站