Пример #1
0
        public void TriggerEventMessage(EventMessage eventMessage, string exChange = null)
        {
            _Context.SendConnection = RabbitMqClientFactory.CreateConnection();

            using (_Context.SendConnection)
            {
                _Context.SendChannel = RabbitMqClientFactory.CreateModel(_Context.SendConnection);

                const byte deliveryModel = 2;

                using (_Context.SendChannel)
                {
                    try
                    {
                        _Context.SendChannel.QueueDeclare(_Context.QueueName, false, false, false, null);
                        var properties = _Context.SendChannel.CreateBasicProperties();
                        properties.DeliveryMode = deliveryModel; //标示 持久化消息

                        //推送消息
                        var body = Encoding.UTF8.GetBytes(eventMessage.JsonBody);
                        _Context.SendChannel.BasicPublish("", _Context.QueueName, properties, body);
                    }
                    catch (Exception ex) { }
                }
            }
        }
Пример #2
0
        private void ListenInit()
        {
            var policy = Policy.Handle <BrokerUnreachableException>()
                         .Or <SocketException>()

                         .WaitAndRetryForever(retrycount => TimeSpan.FromSeconds(retrycount * 2), (ex, time) =>
            {
                //Console.WriteLine($" {time}s ({ex}) 重新创建连接");
            });

            policy.Execute(() =>
            {
                _Context.ListenConnection = RabbitMqClientFactory.CreateConnection();
            });

            _Context.ListenConnection.ConnectionShutdown += (sender, e) =>
            {
                // e.ReplyText;
            };

            _Context.ListenChannel = RabbitMqClientFactory.CreateModel(_Context.ListenConnection);

            var queue = _Context.ListenChannel.QueueDeclare(_Context.QueueName, false, false, false, null);

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

            consumer.Received += consumer_Received;
            _Context.ListenChannel.BasicQos(0, 1, false); //一次只获取一个消息进行消费
            _Context.ListenChannel.BasicConsume(_Context.QueueName, false, consumer);
        }
Пример #3
0
        private void ListenInit()
        {
            _Context.ListenConnection = RabbitMqClientFactory.CreateConnection();
            _Context.ListenConnection.ConnectionShutdown += (sender, e) =>
            {
                // e.ReplyText;
            };

            _Context.ListenChannel = RabbitMqClientFactory.CreateModel(_Context.ListenConnection);

            var queue = _Context.ListenChannel.QueueDeclare(_Context.QueueName, false, false, false, null);

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

            consumer.Received += consumer_Received;
            _Context.ListenChannel.BasicQos(0, 1, false); //一次只获取一个消息进行消费
            _Context.ListenChannel.BasicConsume(_Context.QueueName, false, consumer);
        }