【问题标题】:RabbitMQ with WCF and a persistent queue带有 WCF 和持久队列的 RabbitMQ
【发布时间】:2011-12-22 09:13:12
【问题描述】:

我正在尝试编写一个在rabbitMQ 绑定上工作的wcf 服务。我能够成功创建服务器和客户端,并让客户端通过队列向服务器发送消息。我对 2 个问题感到困惑。

  1. 服务一关闭,队列就会被删除。有没有办法配置 wcf 和 rabbitMQ 以使队列持久化?这样我就不用担心如果我的服务器崩溃了会丢失数据。

  2. 我似乎无法控制队列的名称。当我运行rabbitmqctl.bat list_queues 时,我看到队列被称为amq.gen-3IgZD30XvTEQWNRsezSUUA==。有没有办法控制队列的名字?

【问题讨论】:

    标签: wcf message-queue rabbitmq


    【解决方案1】:

    WCF 绑定无法做到这一点。有关详细信息,请参阅this 邮件列表线程。

    基本上,您无法通过 WCF 控制队列名称,这意味着您仅限于 匿名 队列(就像您所看到的那样),这反过来意味着您只能使用 非持久性队列。

    如果您需要比 WCF 绑定提供的更多控制,您应该考虑使用完整的 .NET 客户端。它非常好用,还有一堆tutorials 可以帮助您入门(它们是用Java 编写的,但.NET API 非常相似)。

    【讨论】:

    • 谢谢。这就是我一直在寻找的答案。如果它不能给你这个控制权,它基本上会让rabbitMQ的WCF绑定变得毫无意义。
    【解决方案2】:

    我遇到了和你一样的问题,我所做的是编辑rabbitMQDotNetClient的源代码。

    文件:RabbitMQInputChannel.cs

        public override void Open(TimeSpan timeout)
        {            
            if (State != CommunicationState.Created && State != CommunicationState.Closed)
                throw new InvalidOperationException(string.Format("Cannot open the channel from the {0} state.", base.State));
    
            OnOpening();
    #if VERBOSE
            DebugHelper.Start();
    #endif
            //Create a queue for messages destined to this service, bind it to the service URI routing key
    #if USE_DEFINED_QUEUE_NAMES
            //here we create a queue that uses the name given in the service address in the wcf binding.
            //if the address in the web.config is: soap.amq:///QueueName
            //the name of the queue will be: QueueName
            //LVV
            string queue = m_model.QueueDeclare(base.LocalAddress.Uri.PathAndQuery, true, false, false, null);
    #else
            string queue = m_model.QueueDeclare();
    #endif
            m_model.QueueBind(queue, Exchange, base.LocalAddress.Uri.PathAndQuery, null);
    
            //Listen to the queue
            m_messageQueue = new QueueingBasicConsumer(m_model);
            m_model.BasicConsume(queue, false, m_messageQueue);
    
    #if VERBOSE
            DebugHelper.Stop(" ## In.Channel.Open {{\n\tAddress={1}, \n\tTime={0}ms}}.", LocalAddress.Uri.PathAndQuery);
    #endif
            OnOpened();
        }
    

    使用标志 USE_DEFINED_QUEUE_NAMES 进行编译。这将使用您在 app.config 或 web.config 文件中指定的名称创建一个队列名称。如果您希望队列的行为与我正在创建的队列不同,您可以随时更改 QueueDeclare(...) 上的队列选项。 干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-09
      • 2016-01-04
      • 2016-01-28
      • 2015-08-03
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      相关资源
      最近更新 更多