一、程序使用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();continue; };
var msg = Encoding.UTF8.GetString(result.Body);
Console.WriteLine(msg);
}
}); task.Wait();
model.Close();
conn.Close();
}
}
}

C#中使用RabbitMQ收发队列消息的更多相关文章

  1. rabbitmq死信队列消息监听

    #邮件通知并发送队列消息#!/bin/bash maillog="/var/log/mq.maillog" message_file="/tmp/mq_message&q ...

  2. python rabbitMQ持久化队列消息

    import pika connection = pika.BlockingConnection( pika.ConnectionParameters('localhost'))#建立一个最基本的so ...

  3. rabbitmq设置队列消息存活时间

    public static final int ALIVETIME = 600000; public static final String QUEUE = "hnyz_gs_queue&q ...

  4. 没用过消息队列?一文带你体验RabbitMQ收发消息

    人生终将是场单人旅途,孤独之前是迷茫,孤独过后是成长. 楔子 先给大家说声抱歉,最近一周都没有发文,有一些比较要紧重要的事需要处理. 今天正好得空,本来说准备写SpringIOC相关的东西,但是发现想 ...

  5. 二、消息队列之如何在C#中使用RabbitMQ

    1.什么是RabbitMQ.详见 http://www.rabbitmq.com/. 作用就是提高系统的并发性,将一些不需要及时响应客户端且占用较多资源的操作,放入队列,再由另外一个线程,去异步处理这 ...

  6. 二、消息队列之如何在C#中使用RabbitMQ(转载)

    二.消息队列之如何在C#中使用RabbitMQ 1.什么是RabbitMQ.详见 http://www.rabbitmq.com/. 作用就是提高系统的并发性,将一些不需要及时响应客户端且占用较多资源 ...

  7. Flume 读取RabbitMq消息队列消息,并将消息写入kafka

    首先是关于flume的基础介绍 组件名称 功能介绍 Agent代理 使用JVM 运行Flume.每台机器运行一个agent,但是可以在一个agent中包含多个sources和sinks. Client ...

  8. RabbitMQ中声明交换器,队列时的,autoDelete=true自动删除的条件

    在声明交换器和队列时,有一个属性叫autoDelete,表示是否自动删除. 如果autoDelete=true,表示自动删除.此处我们要理解,自动删除的条件是什么? 这里的关键是,自动删除的条件是向后 ...

  9. rabbitMQ学习1:消息队列介绍与rabbitmq安装使用

    1. 什么是消息队列 生活里的消息队列,如同邮局的邮箱, 如果没邮箱的话, 邮件必须找到邮件那个人,递给他,才玩完成,那这个任务会处理的很麻烦,很慢,效率很低 但是如果有了邮箱, 邮件直接丢给邮箱,用 ...

随机推荐

  1. /bin/sh^M: bad interpreter:没有那个文件或目录解决

    /bin/sh^M: bad interpreter:没有那个文件或目录解决   执行脚本时发现如下错误: /bin/sh^M: bad interpreter: 没有那个文件或目录   错误分析: ...

  2. C#编程(五十五)----------HashSet和SortedSet

    集 饱含不重复元素的集合称为”集(set)”. .NET4包含两个集(HashSet<T>和SortedSet<T>),他们都实现ISet<T>接口.HashSet ...

  3. Android之文件搜索工具类

    /** * @detail 搜索sdcard文件 * @param 需要进行文件搜索的目录 * @param 过滤搜索文件类型 * */ private void search(File file, ...

  4. 解决 Attempting to destroy the window while drawing!

    当对Dialog进行关闭时,如果有大量的操作,比如动画.绘图什么的,就可能出现这样的错误 Attempting to destroy the window while drawing! 比如,我在自定 ...

  5. LaTeX技巧24:LaTeX常用命令集锦

    \hyphenation{word list} %断字命令:\showthe\topmargin %显示某个参数的数值或者内容: 在tex编译过程中出现行溢出(overflow hbox)是由于断字程 ...

  6. PHP 5.4.17 发布!

    PHP 5.4.17发布.2013-07-04 经过1个RC 上个版本是2013-06-07的5.4.16.修正了大约20个Bug以及几个安全漏洞.尽管5.5.0正式版已经发布.但5.4还未停止更新. ...

  7. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  8. Table does not have the identity property. Cannot perform SET operation.

    Table   does not have the identity property. Cannot perform SET operation. 解决:   set IDENTITY_INSERT ...

  9. verilog语法实例学习(6)

    函数和任务 函数 https://wenku.baidu.com/view/d31d1ba8dd3383c4bb4cd283.html verilog中函数的目的是允许代码写成模块的方式而不是定义独立 ...

  10. WHY数学图形显示工具

    软件功能:输入一个二元数学表达式,含有两个参数变量X和Y,显示该数学表达式的三维图形. 很久之前就有写这个软件的想法,却一直没有激情和动力,终于在年假这两天完成了.以此软件纪念我那十几年前的高中生活, ...