RabbitMQ 官方demo1

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

转载于:https://www.cnblogs.com/ly7454/p/5359433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值