Ejemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="queryConfigKey">配置键</param>
 public RabbitMQHelper(string queryConfigKey = "RabbitMQConfig")
 {
     //创建连接
     connection = RabbitMQClientFactory.CreateConnection(queryConfigKey);
     //创建通道
     channel = connection.CreateModel();
     channel.QueueDeclare(RabbitMQClientFactory.rabbitmq.QueueName, true, false, false, null);
     //声明交换机
     channel.ExchangeDeclare(RabbitMQClientFactory.rabbitmq.ExchangeName, RabbitMQClientFactory.rabbitmq.RoutType, true, false, null);
     //交换机和消息队列绑定
     channel.QueueBind(RabbitMQClientFactory.rabbitmq.QueueName, RabbitMQClientFactory.rabbitmq.ExchangeName, RabbitMQClientFactory.rabbitmq.RoutKey);
 }
Ejemplo n.º 2
0
        public void SendMsg <T>(RabbitMq rabbitMq, T msg)
        {
            //创建连接
            IConnection connection = RabbitMQClientFactory.CreateConnection(rabbitMq);
            //创建通道
            IModel channel = connection.CreateModel();

            //申明队列
            channel.QueueDeclare(rabbitMq.QueueName, true, false, false, null);
            //声明交换机
            channel.ExchangeDeclare(rabbitMq.ExchangeName, rabbitMq.RoutType, true, false, null);
            //交换机和消息队列绑定
            channel.QueueBind(rabbitMq.QueueName, rabbitMq.ExchangeName, rabbitMq.RoutKey, null);
            var basicProperties = channel.CreateBasicProperties();

            //1:非持久化 2:可持久化
            basicProperties.DeliveryMode = 2;
            var payload = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));
            var address = new PublicationAddress(rabbitMq.RoutType, rabbitMq.ExchangeName, rabbitMq.RoutKey);

            channel.BasicPublish(address, basicProperties, payload);
        }