Exemple #1
0
        /// <summary>
        /// 侦听指定的队列
        /// </summary>
        /// <param name="queueName"></param>
        private void ListenInit(string queueName)
        {
            Context.ListenConnection = RabbitMqClientFactory.CreateConnection(queueName); //获取连接

            Context.ListenConnection.ConnectionShutdown += (o, e) =>
            {
                EventLog.Log("SXF.RabbitMQ connection shutdown:" + e.ReplyText, "mq");
            };

            Context.ListenChannel = RabbitMqClientFactory.CreateModel(Context.ListenConnection); //获取通道
            Context.ListenChannel.QueueDeclare(queue: Context.ListenQueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
            var consumer = new EventingBasicConsumer(Context.ListenChannel);                     //创建事件驱动的消费者类型

            consumer.Received += consumer_ReceivedToQueue;

            Context.ListenChannel.BasicQos(0, 1, false); //一次只获取一个消息进行消费
            Context.ListenChannel.BasicConsume(Context.ListenQueueName, false, consumer);
            EventLog.Log("SXF.RabbitMQ侦听指定队列-->end", "mq");
        }
Exemple #2
0
        /// <summary>
        /// 触发一个事件且将事件打包成消息发送到远程队列中。
        /// </summary>
        /// <param name="eventMessage">发送的消息实例。</param>
        /// <param name="exChange">RabbitMq的Exchange名称。</param>
        /// <param name="queue">队列名称。</param>
        public void TriggerEventMessage(EventMessage eventMessage, string exChange, string queue)
        {
            Context.SendConnection = RabbitMqClientFactory.CreateConnection(queue); //获取连接
            //EventLog.Log(exChange, "mq");
            using (Context.SendConnection)
            {
                Context.SendChannel = RabbitMqClientFactory.CreateModel(Context.SendConnection); //获取通道
                Context.SendChannel.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
                const byte deliveryMode = 2;
                using (Context.SendChannel)
                {
                    var messageSerializer = MessageSerializerFactory.CreateMessageSerializerInstance(); //反序列化消息

                    var properties = Context.SendChannel.CreateBasicProperties();
                    properties.DeliveryMode = deliveryMode; //表示持久化消息
                    //EventLog.Log("que:" + queue, "mq");
                    //推送消息
                    Context.SendChannel.BasicPublish(
                        exChange, queue, properties, messageSerializer.SerializerBytes(eventMessage));
                    //EventLog.Log("exchange:" + exChange, "mq");
                    //EventLog.Log("exchange:" + queue, "mq");
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// 创建指定队列的实例
 /// </summary>
 /// <param name="queueName"></param>
 /// <returns></returns>
 public static IRabbitMqClient CreateInstance(string queueName)
 {
     return(RabbitMqClientFactory.CreateRabbitMqClientInstance(queueName));
 }