Ejemplo n.º 1
0
 /// <summary>
 /// 触发一个事件并且将事件打包成消息发送到远程队列中
 /// </summary>
 /// <param name="eventMessage">发送的消息实例</param>
 /// <param name="exChange">Exchange名称</param>
 /// <param name="queue">队列名称</param>
 public void TriggerEventMessage(EventMessage eventMessage, string exChange, string queue)
 {
     using (var connection = RabbitMqClientFactory.CreateConnection())
     {
         //Context.SendChannel = RabbitMqClientFactory.CreateModel(connection);
         const byte deliveryMode = 2;
         using (var channel = RabbitMqClientFactory.CreateModel(connection))
         {
             var messageSerializer = MessageSerializerFactory.CreateMessageSerializerInstance();
             var properties        = channel.CreateBasicProperties();
             properties.DeliveryMode = deliveryMode;
             channel.BasicPublish(exChange, queue, properties,
                                  messageSerializer.SerializerBytes(eventMessage));
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 监听初始化
        /// </summary>
        private void ListenInit()
        {
            Context.ListenConnection = RabbitMqClientFactory.CreateConnection();
            Context.ListenConnection.ConnectionShutdown += (o, e) =>
            {
                Console.WriteLine(e.ReplyText);
            };
            Context.ListenChannel = RabbitMqClientFactory.CreateModel(Context.ListenConnection);

            var consumer = new EventingBasicConsumer(Context.ListenChannel);//创建事件驱动的消费者类型

            consumer.Received += Consumer_Received;

            Context.ListenChannel.BasicQos(0, 1, false);//一次只获取一个消息进行消费
            Context.ListenChannel.BasicConsume(Context.ListenQueueName, false, consumer);
        }