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. BFC的概念及作用

    在了解什么是BFC之前,首先得明白什么是Box , Formatting Context (一个决定如何渲染文档的容器)的概念 Box: CSS布局的基本单位 Box是 CSS 布局的对象和基本单位, ...

  2. Linux基础命令---bc

    bc bc是一种算数语言,其语法和c语言类似,可以交互执行.通过命令行选项可以获得一个标准的数学库.如果请求,在处理任何文件之前定义数学库.BC从处理所有文件的代码开始.命令行中列出的文件按所列顺序排 ...

  3. Linux基础命令---service

    service service可以控制系统服务(打开.关闭.重启).service在尽可能可预测的环境中运行SystemV init脚本,删除大多数环境变量并将当前工作目录设置为根目录.脚本参数位于“ ...

  4. CentOS安装JDK的三种办法

    方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# c ...

  5. mybatis插入语句空值没有设置jdbcType报错

    Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: Error setti ...

  6. Django框架----视图函数补充

    视图函数的补充 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象)   request对象:----->所有的请求信息   HttpResponse:-- ...

  7. php多进程结合Linux利器split命令实现把大文件分批高效处理

    有时候会遇到这样的需求,比如log日志文件,这个文件很大,甚至上百M,需要把所有的日志拿来做统计,这时候我们如果用单进程来处理,效率会很慢.如果我们想要快速完成这项需求,我们可以利用Linux的一个利 ...

  8. cache与buffer

    Cache 缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而  Cache保存着CPU刚用过的 ...

  9. assert_param

    在STM32的固件库和提供的例程中,到处都可以见到assert_param()的使用.如果打开任何一个例程中的stm32f10x_conf.h文件,就可以看到实际上assert_param是一个宏定义 ...

  10. 怎样用QtCreator编辑运行python脚本

    QtCreator作为一款开发基于qt库的程序.以及开发C语言.c++语言项目都是一个利器,轻便好用.那么作为开发者来说,经常换着使用各种IDE是家常便饭,但是要是这些语言都能够集成到一个工具里岂不是 ...