C#RabbitMQ消息队列

本文介绍了一个使用RabbitMQ实现的消息队列示例,包括发送端和接收端的代码实现。发送端通过创建连接和通道,声明队列并发送消息;接收端则通过监听队列来接收消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Sending 发送端

都需要安装RabbitMQ.Client NuGet包

 using System;
using System.Text;
using RabbitMQ.Client;

namespace Sending
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建连接工厂
            var factory = new ConnectionFactory()
            {
                HostName = "39.99.140.121",
                Port = 5672,
                UserName = "test",
                Password = "test",
                VirtualHost = "/zsq"
            };

            //创建连接
            using (var connection = factory.CreateConnection())

            //创建通道
            using (var channel = connection.CreateModel())
            {
                //创建一个名叫"hello"的消息队列
                channel.QueueDeclare(
                    queue: "hello",
                    durable: false,
                    exclusive: false,
                    autoDelete: false,
                    arguments: null);

                string message = "Holle World";

                var body = Encoding.UTF8.GetBytes(message);

                //向消息队列发送消息message
                channel.BasicPublish(
                    exchange: "",
                    routingKey: "hello",
                    basicProperties: null,
                    body: body);
                Console.WriteLine("[x] sent {0}", message);
            }

            Console.WriteLine("Press [enter] to exit.");
            Console.ReadLine();
        }
    }
}

2.Receiving 接受端

using System;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace Receiving
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建连接工厂
            var factory = new ConnectionFactory()
            {
                HostName = "39.99.140.121",
                Port = 5672,
                UserName = "test",
                Password = "test",
                VirtualHost = "/zsq"
            };

            //创建连接
           using (var connection = factory.CreateConnection()) 
            //创建通道
           using (var channel = connection.CreateModel())
           {
               //创建一个名为"hello"的队列,防止producer端没有创建该队列
               channel.QueueDeclare(
                   queue: "hello",
                   durable: false,
                   exclusive: false,
                   autoDelete: false,
                   arguments: null);

               //回调,当Receiving收到消息后就会执行该函数
               var consumer = new EventingBasicConsumer(channel);
               consumer.Received += (model, ea) =>
               {
                   var body = ea.Body.ToArray();
                   var message = Encoding.UTF8.GetString(body);
                   Console.WriteLine("[x] Received {0}", message);
               };

               //消费队列中"hello"的消息
               channel.BasicConsume(
                   queue: "hello",
                   autoAck: true,
                   consumer: consumer);

               Console.WriteLine(" Press [enter] to exit.");
               Console.ReadLine();
           }
        }
    }
}

3.参考

链接: https://zhuanlan.zhihu.com/p/54986437
链接: https://www.cnblogs.com/mq0036/p/11726733.html
链接: https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值