需要帮助创建RabbitMQ使用者作为Windows服务?

时间:2014-12-31 09:27:12

标签: rabbitmq amqp

此代码适用于控制台应用程序,但它不适用于Windows服务应用程序。 //从队列接收消息的代码

 public void ReceiveMessage()
    {
        const string EXCHANGE_NAME = "EXCHANGE3";
        ConnectionFactory factory = new ConnectionFactory();
       //Rabbit MQ connection
        using (IConnection connection = factory.CreateConnection())
        {
            using (IModel channel = connection.CreateModel())
            {
                channel.QueueDeclare("task_queue", true, false, false, null);

                channel.BasicQos(0, 1, false);
                var consumer = new QueueingBasicConsumer(channel);
                channel.BasicConsume("task_queue", false, consumer);
                //Waiting in loop to get message 
                while (true)
                {
                    var ea =
                        (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                    Object message = Desserialize(ea.Body);


                   //Acknowledge one or more delivered message
                    channel.BasicAck(ea.DeliveryTag, false);
                }

            }
        }
    }

0 个答案:

没有答案