首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >EasyNetQ未呼叫用户

EasyNetQ未呼叫用户
EN

Stack Overflow用户
提问于 2013-04-30 03:30:26
回答 3查看 2.1K关注 0票数 1

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

代码语言:javascript
运行
复制
    //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);
    }
EN

Stack Overflow用户

发布于 2013-04-30 04:07:02

好吧,我找到了一个解决这个问题的简单方法。最后,我搜索了EasyNetQ应用程序接口中的可用方法,找到了Queue.SetAsSingleUse()。在将队列绑定到交换之后,我简单地调用了该方法,这基本上就完成了。不确定queue.SetAsSingleUse()是做什么的,但从它的名字来看,它听起来像是在发布第一条消息并将其传递给订阅者之后处理队列。在我的例子中,每次测试只使用一次队列是有意义的,但是出于某种原因想要保留队列的人可能会遇到麻烦。

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16286712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档