コード例 #1
0
ファイル: Program.cs プロジェクト: hadacduong5394/MQDemo
        private static void PullMQ()
        {
            Console.WriteLine("Begin pull all msg...");

            var MQQueueName = ConfigurationManager.AppSettings["MQQueueName"];
            var factory     = MQUtils.MQFactory;

            using (var conn = factory.CreateConnection())
            {
                using (RabbitMQ.Client.IModel channel = conn.CreateModel())
                {
                    IDictionary <string, object> queueArgs = new Dictionary <string, object>
                    {
                        { "x-ha-policy", "all" }
                    };
                    channel.QueueDeclare(queue: MQQueueName,
                                         durable: true,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: queueArgs);
                    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
                    while (!MQUtils.QueueEmpty())
                    {
                        if (!MQUtils.Pull(channel, ExcuteMessage))
                        {
                            Console.WriteLine("{0} - Queue is empty!", Thread.CurrentThread.ManagedThreadId);
                            Thread.Sleep(1000);
                        }
                    }
                    Console.WriteLine("pull all msg done!");
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: hadacduong5394/MQDemo
        private static bool PushMQ(int numberMsg)
        {
            int i = 1;

            while (i < numberMsg)
            {
                int count   = 0;
                var message = new MQMessage(i);
                while (!MQUtils.Push(message))
                {
                    Console.WriteLine("{0} Có lỗi khi gửi lên queue. Thử lại lần {1}", i, count++);
                    Thread.Sleep(3000);
                }
                Console.WriteLine(" [x] Sent {0}", i++);
            }
            Console.WriteLine("Push all msg done!");
            return(true);
        }