zoukankan      html  css  js  c++  java
  • RabbitMQ C# 例子 -摘自网络

      //刚刚接触,如有不对还望不吝指正
            public static void StartUp()
            {
                #region 前期准备工作
     
                ConnectionFactory factory = new ConnectionFactory();
                factory.Uri = "amqp://guest:guest@localhost:5672/";
                //架起物理链路
                IConnection conn = factory.CreateConnection();
                //创建通信信道
                IModel channel = conn.CreateModel();
     
                string exchangename = "我是负责转发信号的路由1";
                string queuename = "我是信号的接收器1";
                //架设信号路由
                channel.ExchangeDeclare(exchangename, ExchangeType.Direct);
                //架设信号接收器
                channel.QueueDeclare(queuename, false, false, false, null);
                //连通信号路由器和接收器
                channel.QueueBind(queuename, exchangename, "", null);
     
                #endregion
     
                byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
                //channel.BasicPublish(exchangename,"",null,messageBodyBytes);
                IBasicProperties props = channel.CreateBasicProperties();
                props.ContentType = "text/plain";
                props.DeliveryMode = 2;
                channel.BasicPublish(exchangename,"",props,messageBodyBytes);
     
     
                ConnectionFactory service = new ConnectionFactory();
     
                using (conn = service.CreateConnection())
                {
                    using (IModel servicechannle = conn.CreateModel())
                    {
                        //ch.QueueDeclare(queuename, false, false, false, null);
                        //服务端接收消息队列
                        bool noAck = false;
                        BasicGetResult result = servicechannle.BasicGet(queuename, noAck);
                        if (result == null)
                        {
                            Console.WriteLine("没有消息");
                        }
                        else
                        {
                            servicechannle.BasicAck(result.DeliveryTag, false);
                            Console.WriteLine("接收消息成功:" + result.BasicProperties.ContentType);
                            Console.WriteLine("接收消息成功:" + result.BasicProperties.DeliveryMode);
                            Console.WriteLine("接收消息成功:" + System.Text.Encoding.Default.GetString(result.Body));
                        }
                    }
                }
     
     
                conn.AutoClose = true;
            }
    

      

  • 相关阅读:
    Linux Home目录硬盘空间缩减
    test
    ORACLE 数据泵 expdp/impdp
    mysql利用mysqlbinlog命令恢复误删除数据
    LogMiner日志挖掘分析管理
    Oracle 审计测试与总结
    redis 5.0.3 讲解、集群搭建
    联想服务器配置 RAID
    Cenots7对lvm逻辑卷分区大小的调整
    kvm 基本运维命令
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/4401914.html
Copyright © 2011-2022 走看看