예제 #1
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(); //获取连接

            using (Context.SendConnection)
            {
                Context.SendChannel = RabbitMqClientFactory.CreateModel(Context.SendConnection); //获取通道

                const byte deliveryMode = 2;
                using (Context.SendChannel)
                {
                    var messageSerializer = MessageSerializerFactory.CreateMessageSerializerInstance(); //反序列化消息

                    var properties = Context.SendChannel.CreateBasicProperties();
                    properties.DeliveryMode = deliveryMode; //表示持久化消息

                    //推送消息
                    Context.SendChannel.BasicPublish(exChange, queue, true, properties, messageSerializer.SerializerBytes(eventMessage));
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 侦听初始化。
        /// </summary>
        private void ListenInit()
        {
            Context.ListenConnection = RabbitMqClientFactory.CreateConnection(); //获取连接

            Context.ListenConnection.ConnectionShutdown += (o, e) =>
            {
                if (LogLocation.Log.IsNotNull())
                {
                    LogLocation.Log.WriteInfo("SCM.RabbitMQClient", "connection shutdown:" + 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);
        }