在特定时间后,在RabbitMQ中将数据从一个队列传输到另一个队列

时间:2018-02-27 10:31:12

标签: c# rabbitmq

以下是创建两个RabbitMQ的代码,并尝试首先在一个队列中添加3个列表项,然后将数据从一个队列中删除到另一个队列但不起作用。我在这里失踪的地方。 我参考了以下网址 https://medium.com/@kiennguyen88/rabbitmq-delay-retry-schedule-with-dead-letter-exchange-31fb25a440fc

 private const string WORK_QUEUE = "LoanBeamQueue";
        private const string WORK_EXCHANGE = "LoanBeamExchange"; // dead letter exchange

        private const string RETRY_EXCHANGE = "DeadLetterExchange";
        private const string WAIT_QUEUE = "DeadLetterQueue";
        private const int RETRY_DELAY = 300000; // in ms

        static void Main(string[] args)
        {
            try
            {
                // Declare the queue name
                // Create a new connection factory for the queue
                var factory = new ConnectionFactory();

                // Because Rabbit is installed locally, we can run it on localhost
                factory.HostName = ConnectionConstants.HostName;
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    // When reading from a persistent queue, you need to tell that to your consumer

                    var queueArgs = new Dictionary<string, object> {
    { "x-dead-letter-exchange", WORK_EXCHANGE },
    { "x-message-ttl", RETRY_DELAY }
};
                    const bool durable = true;
                    //channel.QueueDeclare(WORK_QUEUE, durable, false, false, null);
                    //channel.QueueDeclare(WAIT_QUEUE, durable, false, false, queueArgs);

                    #region Seperate
                    channel.ExchangeDeclare(WORK_EXCHANGE, "direct");
                    channel.QueueDeclare(WORK_QUEUE, true, false, false, null);
                    channel.QueueBind(WORK_QUEUE, WORK_EXCHANGE, string.Empty, null);

                    channel.ExchangeDeclare(RETRY_EXCHANGE, "direct");
                    channel.QueueDeclare(WAIT_QUEUE, true, false, false, queueArgs);
                    channel.QueueBind(WAIT_QUEUE, RETRY_EXCHANGE, string.Empty, null);
                    #endregion

                    #region NewAdded
                    List<string> lstDemoID2 = new List<string>();
                    lstDemoID2.Add("1");
                    lstDemoID2.Add("2");
                    lstDemoID2.Add("3");
                    foreach (var message in lstDemoID2)
                    {
                        var body = Encoding.UTF8.GetBytes(message);
                        //using default exchange
                        channel.BasicPublish(exchange: "", routingKey: "LoanBeamQueue", basicProperties: null, body: body);

                        Console.WriteLine("Sent ApplicationID {0}", message);
                    }
                    #endregion
                    // turn auto acknowledge off so we can do it manually. This is so we don't remove items from the queue until we're perfectly happy
                    //var consumer = new QueueingBasicConsumer(channel);
                    //const bool autoAck = false;
                    //channel.BasicConsume(WORK_QUEUE, true, consumer);
                    QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(WORK_QUEUE, true, consumer);                      
                }
            }

0 个答案:

没有答案