博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中使用RabbitMQ收发队列消息
阅读量:6295 次
发布时间:2019-06-22

本文共 3039 字,大约阅读时间需要 10 分钟。

一、程序使用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();        }    }}

 

 

转载地址:http://dmpta.baihongyu.com/

你可能感兴趣的文章
Ucenter 会员同步登录通讯原理
查看>>
php--------获取当前时间、时间戳
查看>>
Spring MVC中文文档翻译发布
查看>>
docker centos环境部署tomcat
查看>>
JavaScript 基础(九): 条件 语句
查看>>
Linux系统固定IP配置
查看>>
配置Quartz
查看>>
Linux 线程实现机制分析
查看>>
继承自ActionBarActivity的activity的activity theme问题
查看>>
设计模式01:简单工厂模式
查看>>
项目经理笔记一
查看>>
Hibernate一对一外键双向关联
查看>>
mac pro 入手,php环境配置总结
查看>>
MyBatis-Plus | 最简单的查询操作教程(Lambda)
查看>>
rpmfusion 的国内大学 NEU 源配置
查看>>
spring jpa 配置详解
查看>>
IOE,为什么去IOE?
查看>>
Storm中的Worker
查看>>
dangdang.ddframe.job中页面修改表达式后进行检查
查看>>
Web基础架构:负载均衡和LVS
查看>>