Пример #1
0
        public void DelaySender(MessageData message, int delay_time)
        {
            Context.SendConnection = RabbitMQClientFactory.CreateConnection(Config);
            using (Context.SendConnection)
            {
                Context.SendChannel = RabbitMQClientFactory.CreateChannel(Context.SendConnection);
                using (Context.SendChannel)
                {
                    string queue     = "test_ttl_queue";
                    string exchange  = "test_ttl_exchange";
                    string route_key = "test_ttl_routekey";

                    RabbitMQClientFactory.ExchangeDeclare(Context.SendChannel, exchange, "direct", false, false);
                    Dictionary <string, object> arg = new Dictionary <string, object>();
                    arg.Add("x-dead-letter-exchange", "test_ttl_exchange.delay");
                    arg.Add("x-dead-letter-routing-key", "test_ttl_route.delay");
                    RabbitMQClientFactory.QueueDeclare(Context.SendChannel, queue, true, false, false, arg);
                    RabbitMQClientFactory.QueueBind(Context.SendChannel, queue, exchange, route_key);

                    byte[]           msg_byte   = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
                    IBasicProperties properties = Context.SendChannel.CreateBasicProperties();
                    properties.DeliveryMode = 2;                     //持久化消息
                    properties.Expiration   = delay_time.ToString(); //消息过期时间
                    Context.SendChannel.BasicPublish(exchange, route_key, properties, msg_byte);
                }
            }
        }
Пример #2
0
        public void Sender(MessageData message)
        {
            Context.SendConnection = RabbitMQClientFactory.CreateConnection(Config);
            using (Context.SendConnection)
            {
                Context.SendChannel = RabbitMQClientFactory.CreateChannel(Context.SendConnection);
                using (Context.SendChannel)
                {
                    RabbitMQClientFactory.ExchangeDeclare(Context.SendChannel, Config.ExchangeName, "direct", false, false);
                    RabbitMQClientFactory.QueueDeclare(Context.SendChannel, Config.QueueName, true, false, false);
                    RabbitMQClientFactory.QueueBind(Context.SendChannel, Config.QueueName, Config.ExchangeName, Config.RouteKey);

                    byte[]           msg_byte   = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
                    IBasicProperties properties = Context.SendChannel.CreateBasicProperties();
                    properties.DeliveryMode = 2;    //持久化消息
                    Context.SendChannel.BasicPublish(Config.ExchangeName, Config.RouteKey, properties, msg_byte);
                }
            }
        }
Пример #3
0
        private void DelayReceiver()
        {
            Context.ReceiveConnection = RabbitMQClientFactory.CreateConnection(Config);
            Context.ReceiveConnection.ConnectionShutdown += (o, e) =>
            {
                return;
            };

            Context.ReceiveChannel = RabbitMQClientFactory.CreateChannel(Context.ReceiveConnection);

            string delay_exchange = "test_ttl_exchange.delay";
            string delay_routekey = "test_ttl_route.delay";
            string delay_queue    = "test_ttl_queue.delay";

            RabbitMQClientFactory.ExchangeDeclare(Context.ReceiveChannel, delay_exchange, "direct", false, false);
            RabbitMQClientFactory.QueueDeclare(Context.ReceiveChannel, delay_queue, true, false, false, null);
            RabbitMQClientFactory.QueueBind(Context.ReceiveChannel, delay_queue, delay_exchange, delay_routekey);

            var consumer = new EventingBasicConsumer(Context.ReceiveChannel);

            consumer.Received += Consumer;
            Context.ReceiveChannel.BasicQos(0, 10, false);
            Context.ReceiveChannel.BasicConsume(delay_queue, false, consumer);
        }