EasyNetQ没有呼叫用户

时间:2013-04-29 19:30:26

标签: asp.net-mvc asynchronous rabbitmq subscriber easynetq

我有一个ASP.NET MVC操作,它验证连接字符串是否正常工作以使用RabbitMQ连接到服务器。我是如何做到这一点的,只需创建一个队列,订阅它然后立即向它发布消息。我希望这条消息最多会在几秒钟后出现在我的用户身上,但事实并非如此。订户仅在我第一次调用操作时收到通知(在我使用其基于Web的管理器从RabbitMQ中删除队列之后),但是一旦我发布了已创建队列的任何后续消息,则无法调用订阅者。请看看我的代码,如果你看到我没看到的东西,请告诉我。提前谢谢。

    //This is just the action called on a POST request.
    //Here is where the test is done.
    [HttpPost]
    public void DoConnectionVerificationAsync()
    {
        const string paramName = "queueSuccessful";
        try
        {
            //This is my routing key
            const string routingKey = "management.verify";

            //Here I declare the key and the exchange and bind them.
            var queue = Queue.DeclareDurable(routingKey);
            var exchange = Exchange.DeclareDirect("Orchard");
            queue.BindTo(exchange, routingKey);

            //Here I just generate a random int to send in the test message to the queue
            Random random = new Random();
            int randomInt = random.Next();
            //Instantiate the actual message with the random integer.
            var message = new Message<VerifyMessage>(new VerifyMessage
            {
                Content = randomInt.ToString(CultureInfo.InvariantCulture)
            });
            message.Properties.AppId = "CRM";

            //Because this is an asynchronous action, here I hint the beginning of the async operation.
            AsyncManager.OutstandingOperations.Increment();

            //Here I have my subscriber. The subscriber gets called only the first time, when the queue hasn't been created yet. That's a problem, it should be called every time I publish, which is in the following lines of code.
            _bus.Subscribe<VerifyMessage>(queue, (response, messageReceivedInfo) => Task.Factory.StartNew(() =>
            {
                VerifyMessage receivedMessage = response.Body;
                string content = receivedMessage.Content;
                int integer = int.Parse(content);
                //I expect the int received from the queue is the same as the one I sent
                bool success = integer == randomInt;
                AsyncManager.Parameters[paramName] = success;
                AsyncManager.OutstandingOperations.Decrement();
            }));

            //And here I publish the message. This always works, I can see the message stored in the queue using the web based RabbitMQ Manager
            //The problem is that the message gets stuck in the queue and never gets sent to the subscriber defined above
            using (var publishChannel = _bus.OpenPublishChannel(x => x.WithPublisherConfirms()))
            {
                publishChannel.Publish(exchange, routingKey, message, t =>
                    t.OnSuccess(() =>
                        {
                            // If we successfully publish, then there's nothing we really need to do. So this function stays empty.
                        })
                        .OnFailure(() =>
                            {
                                AsyncManager.Parameters[paramName] = false; AsyncManager.OutstandingOperations.Decrement();
                            }));
            }
        }
        catch (EasyNetQException)
        {
            AsyncManager.Parameters[paramName] = false;
            AsyncManager.OutstandingOperations.Decrement();
        }
    }

    //These functions down here don't really matter, but I'm including them just in case so that you can see it all.
    public ActionResult DoConnectionVerificationCompleted(bool queueSuccessful)
    {
        return RedirectToAction("VerifyQueueConnectionResult", new { queueSuccessful });
    }

    public ActionResult VerifyQueueConnectionResult(bool queueSuccessful)
    {
        VerifyQueueConnectionResultModel model = new VerifyQueueConnectionResultModel();
        model.Succeded = queueSuccessful;
        return View(model);
    }

3 个答案:

答案 0 :(得分:2)

每次调用bus.Subscribe都会创建一个新的消费者。因此,第一次使用时,您的队列中只有一个消费者,当您向其发布消息时,它将路由到该消费者。

第二次调用bus.Subscribe第二个使用者绑定到同一队列。当RabbitMQ在单个队列上有多个消费者时,它会向消费者循环消息。这是一个功能。这就是它开箱即用的工作方式。您的邮件将路由到第一个使用者,而不是您刚刚声明的使用者。对于第3,第4等消费者来说也是如此,因此消息似乎没有到达。

答案 1 :(得分:0)

好吧,我找到了解决这个问题的简单方法。我最终搜索了EasyNetQ API中的可用方法,找到了Queue.SetAsSingleUse()。我只是在将队列绑定到交换机之后调用该方法,而且几乎就是这样做的。不确定queue.SetAsSingleUse()是做什么的,但通过它的名称,它听起来像是在第一条消息发布并传递给订阅者之后处理队列。就我而言,每次测试只使用一次队列是有意义的,但是出于某种原因可能想要保留队列的人可能会遇到麻烦。

答案 2 :(得分:0)

您可以为每个不同的订阅者使用不同的队列,如下所示:

bus.Subscribe<MyMessage>("my_subscription_1"...
bus.Subscribe<MyMessage>("my_subscription_2"...

这样,您发布的MyMessage类型的任何消息都将到达所有这些队列。