• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中使用RabbitMQ收发队列消息

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、程序使用NetCore、引入Nuget:

  Install-Package RabbitMQ.Client -Version 4.1.3

二、消息发部端:

  

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

namespace ClientDemo
{
    public class Client
    {
        static string exchangeName = "my-exchange";
        static string queueName = "my-queue";
        public static void Main()
        {
            Console.InputEncoding = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;
            ConnectionFactory factory = new ConnectionFactory();
            factory.Uri = new Uri("amqp://guest:guest@localhost:5672/");
            var conn = factory.CreateConnection();
            IModel model = conn.CreateModel();

            //model.ExchangeDelete(exchangeName);
            model.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Direct, durable: false, autoDelete: false, arguments: null);
            model.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
            model.QueueBind(queue: queueName, exchange: exchangeName, routingKey: queueName);

            var props = model.CreateBasicProperties();
            props.Persistent = true;//是否持久化
            while (true)
            {
                Console.WriteLine("请输入要发送的消息:");
                var line = Console.ReadLine();
                if (line == "exit") break;
                model.BasicPublish(exchange: exchangeName, routingKey: queueName, basicProperties: props, body: Encoding.UTF8.GetBytes(line));

            }
            model.Close();
            conn.Close();
        }
    }
}

 

 二、消息消费端:

using RabbitMQ.Client;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerDemo
{
    public class Server
    {
        //static string exchangeName = "my-exchange";
        static string queueName = "my-queue";
        public static void Main()
        {
            Console.InputEncoding = Encoding.Unicode;
            Console.OutputEncoding = Encoding.Unicode;
            ConnectionFactory factory = new ConnectionFactory();
            //factory.Uri = new Uri("amqp://guest:guest@localhost:5672/");
            var conn = factory.CreateConnection();
            IModel model = conn.CreateModel();

            //model.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Direct, durable: false, autoDelete: false, arguments: null);
            //model.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
            //model.QueueBind(queue: queueName, exchange: exchangeName, routingKey: queueName);
            var task = Task.Run(() =>
             {
                 while (true)
                 {
                     var result = model.BasicGet(queue: queueName, autoAck: false);
                     if (result == null) { Thread.Sleep(10);continue; };
                     var msg = Encoding.UTF8.GetString(result.Body);
                     Console.WriteLine(msg);
                 }
             });

            task.Wait();
            model.Close();
            conn.Close();
        }
    }
}

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#反射/映射学习发布时间:2022-07-14
下一篇:
C盘清理小程序制作发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap