1、通过Nuget 获取Rabbit MQ NET client bindings from NuGet:

PM> Install-Package RabbitMQ.Client

2、发送者(生产者)代码:

 public class RabbitMqSender
{
private static string strHostName = "localhost";
private static string strExchangeName = "test.exchange.1";
private static string strQueueName = "test.queue.1";
private static string strRouteKey = "test.exchange.key.1";
public static void send()
{ var factory = new ConnectionFactory() { HostName= strHostName };
using (var connection=factory.CreateConnection())
{
using (var channel=connection.CreateModel())
{
//channel.QueueDeclare
// (queue: strQueueName,
// durable: true,
// exclusive: false,
// autoDelete: false,
// arguments: null
// );
//channel.ExchangeDeclare(exchange: strQueueName,type:"direct", durable: true, autoDelete: false, arguments: null); channel.ExchangeDeclare(strExchangeName, ExchangeType.Direct, true, false);
channel.QueueDeclare(strQueueName, true, false, false, null);
channel.QueueBind(strQueueName, strExchangeName, strRouteKey, null);
string message = string.Empty;
byte[] body = null;
for (int i = ; i < ; i++)
{
message = i+DateTime.Now.ToString() + " -----send rabbitmq message!";
body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(
exchange: strExchangeName,
routingKey: strRouteKey,
basicProperties: null,
body: body
);
Console.WriteLine("{0}", message);
}
} }
}
}

3、接收者(消费者)代码

public class RabbitMqReceiver
{
private static string strHostName = "localhost";
private static string strExchangeName = "test.exchange.1";
private static string strQueueName = "test.queue.1";
private static string strRouteKey = "test.exchange.key.1";
public static void receive()
{
var factory = new ConnectionFactory() { HostName = strHostName };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{ //channel.ExchangeDeclare(exchange: strQueueName, type: "direct", durable: true, autoDelete: false, arguments: null);
channel.ExchangeDeclare(strExchangeName, ExchangeType.Direct, true, false);
channel.QueueDeclare(strQueueName, true, false, false, null);
channel.QueueBind(strQueueName, strExchangeName, strRouteKey, null); var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("AAAAAAAAA--- [x] Received {0}", message);
};
channel.BasicConsume(queue: strQueueName,
noAck: true,
consumer: consumer); //BasicGetResult result = channel.BasicGet(queue: "test.queue", noAck: true);
//while (result!= null)
//{
// string strMessage = Encoding.UTF8.GetString(result.Body);
// Console.WriteLine(" [x] Received {0}", strMessage);
// result = channel.BasicGet(queue: "test.queue", noAck: true);
//}
Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); }
}
}

4、运行效果

.NET中使用Rabbit MQ的更多相关文章

  1. redis反向代理docker容器中的rabbit mq服务

    最近做的项目中用到了docker,发现docker容器还真挺好用的,可以统一来管理各种资源,项目. 但是在实际使用中就碰到下面这个问题(下面所有的应用都是在docker中启动的): 通过nginx来反 ...

  2. spring boot 中 rabbit mq基础例子

    1.先安装ELANG,再按照RabbitMQ 2.打开RabbitMQ控制台:rabbit command prompt 1.设置elang的路径:set ERLANG_HOME=D:\work_pr ...

  3. 在 Windows 上安装Rabbit MQ 指南

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.他遵循Mozilla Public License开源协议.采用 Erlang 实现的工业级的消息队列(MQ)服务器. Ra ...

  4. windows下的php rabbit mq安装、配置

    http://www.cnblogs.com/shanyou/p/4067250.html 这篇博文写的rabbit mq和erlang的安装以及rabbitmq可视化插件的一些操作 接下去开始安装P ...

  5. (转)在 Windows 上安装Rabbit MQ 指南

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.他遵循Mozilla Public License开源协议.采用 Erlang 实现的工业级的消息队列(MQ)服务器. Ra ...

  6. celery rabbit mq 详解

    Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery, ...

  7. Rabbit MQ 消息确认和持久化机制

    一:确认种类 RabbitMQ的消息确认有两种.一种是消息发送确认,用来确认生产者将消息发送给交换器,交换器传递给队列的过程中消息是否成功投递.发送确认分为两步,一是确认是否到达交换器,二是确认是否到 ...

  8. celery+Rabbit MQ实战记录

    基于以前的一篇文章,celery+Rabbit MQ的安装和使用, 本文更加详细的介绍如何安装和使用celey, Rabbit MQ. 并记录在使用celery时遇到的一些问题. 1.安装 Rabbi ...

  9. Rabbit MQ 入门指南

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.他遵循Mozilla Public License开源协议.采用 Erlang 实现的工业级的消息队列(MQ)服务器. Ra ...

随机推荐

  1. Linux下DNS服务器配置

    一步:yum install -y bind bind-utils bind-chroot yum install bind* //安装DNS服务 第二步:systemctl stop firewal ...

  2. Linux服务器配置---ntp

    配置ntp     ntp就是网络时间同步的服务,时间的准确性非常重要,很多数据在记录时都要知道准确的时间.网上有很多站点,一般国内会设置匹配中科院国家授时中心的时间. 1.安装ntp软件 [root ...

  3. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  4. netty4----日志框架的检查

    https://segmentfault.com/a/1190000005797595 2016年06月25日  ·  4.1k 次阅读 Netty4.x Internal Logger机制 nett ...

  5. div居中布局

    利用margin属性可以实现div居中布局,把div的左边距和右边距设置为auto即可,代码如下 <!DOCTYPE html> <html> <head> < ...

  6. (二) MySQL常用命令及语法规范

  7. 对于“机器视觉(computer version)”的反思

    做图像有一段时间了,几个问题进行反思,欢迎讨论 1.机器视觉的本质是什么? 我认为就是通过计算机和数学的方法,对一定形式存储的2d或3d的视觉信号进行增强.延伸,以增加信号的强度: 2.机器视觉的第一 ...

  8. 2018-2019-1 20189218《Linux内核原理与分析》第五周作业

    系统调用的三层机制 用户态.内核态和中断 用户态.较低的执行级别,只能访问一部分内存,只能执行一部分指令. 内核态.高级执行级别,可以访问任意物理内存,可以执行特权指令. 中断.系统从用户态进入内核态 ...

  9. FSMC(STM32)

    (一)FSMC:Flexible Static Memory Controller,可变(灵活)静态存储控制器 小容量产品是指闪存存储器容量在1 6K至32K 字节之间的STM32F101xx.STM ...

  10. linux 之awk命令详解

    awk是一种程序语言,对文档资料的处理具有很强的功能.awk名称是由它三个最初设计者的姓氏的第一个字母而命名的: Alfred V. Aho.Peter J. We i n b e rg e r.Br ...