public class RabbitMqSend
{
public static void Test()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var conn = factory.CreateConnection())
using(var channel = conn.CreateModel())
{
channel.QueueDeclare(queue: "hello", durable: true, exclusive: false, autoDelete: false, arguments: null);
//消息持久化,durable: true和.Persistent = true 2个都要设置
//durable: true-->needs to be applied to both the producer and consumer code.
var properties = channel.CreateBasicProperties();
properties.Persistent = true; string msg = "你好,老大!!!";
var body = Encoding.UTF8.GetBytes(msg);
channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: properties, body: body);
Console.WriteLine("[x] sent {0}", msg);
}
}
    public class RabbitMqTest
{
public static void Test()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
//息持久化,durable: true
channel.QueueDeclare(queue: "hello",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var msg = Encoding.UTF8.GetString(body);
Console.WriteLine("[x] received {0}", msg);
//发送确认消息,通知改消息处理完成,可以删除
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
//noAck = false 表示需求确认消息已经处理才在服务器中删除,进入下一个消息
channel.BasicConsume(queue: "hello", noAck: false, consumer: consumer);
}
}
/*
* Forgotten acknowledgment
It's a common mistake to miss the BasicAck. It's an easy error, but the consequences are serious.
Messages will be redelivered when your client quits (which may look like random redelivery),
but RabbitMQ will eat more and more memory as it won't be able to release any unacked messages. In order to debug this kind of mistake you can use rabbitmqctl to print the messages_unacknowledged field: $ sudo rabbitmqctl list_queues name messages_ready messages_unacknowledged
Listing queues ...
hello 0 0
...done.
*/
}

启动mq服务:rabbitmq-service.bat

最新文章

  1. Python_猜大小
  2. AngularJS之Scope及Controller(一)
  3. Symantec Backup Exec 2010 Agent For Linux安装
  4. 如何用 .Net 开发
  5. ADO.net操作数据库
  6. 东大OJ-1391-Big big Power
  7. 暑假集训(2)第六弹 ----- Frosh Week(UVA11858)
  8. 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.
  9. WPF学习(一)控件的公共属性
  10. cocos2d-x-2.2.5项目创建--命令行创建
  11. 《连载 | 物联网框架ServerSuperIO教程》- 18.集成OPC Client,及使用步骤
  12. cocos2dx - 环境配置,项目创建
  13. ajax请求获取实时数据
  14. NOIP2017+停课总结
  15. 使用django发布带图片的网页(上)
  16. Atitit s2018.2 s2 doc list on home ntpc.docx  \Atiitt uke制度体系 法律 法规 规章 条例 国王诏书.docx \Atiitt 手写文字识别 讯飞科大 语音云.docx \Atitit 代码托管与虚拟主机.docx \Atitit 企业文化 每日心灵 鸡汤 值班 发布.docx \Atitit 几大研发体系对比 Stage-Gat
  17. Android之一种很有趣的界面跳动提示动画
  18. 通过设置P3P头来实现跨域访问COOKIE
  19. Centos6版本使用yum报错 Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfi Setting up Install Process No package gcc available. Error: Nothing to do
  20. LOJ#2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On

热门文章

  1. 修改elementui默认样式
  2. Django commands自定制
  3. sql的四种匹配模式
  4. Css - 选择器和样式
  5. IDEA - Debug - not supported in -source 1.5
  6. 在django admin中添加自定义视图
  7. Linux下查看CPU型号,内存大小,硬盘空间,进程等的命令(详解)
  8. debug 2
  9. springboo+nginx测试反向代理01
  10. L1-049 天梯赛座位分配​​​​​​​