简体   繁体   English

带有WCF和持久队列的RabbitMQ

[英]RabbitMQ with WCF and a persistent queue

I am trying to write a wcf service that works over the rabbitMQ binding. 我正在尝试编写一个适用于rabbitMQ绑定的wcf服务。 I was able to successfully create a server and a client and have the client send messages to the server via the queue. 我能够成功创建服务器和客户端,并让客户端通过队列向服务器发送消息。 I am confused about 2 issues. 我对2个问题很困惑。

  1. As soon as the service is shutdown the queue is deleted. 服务一旦关闭,队列就会被删除。 Is there a way to configure wcf and rabbitMQ so that the queue is persistent? 有没有办法配置wcf和rabbitMQ,以便队列是持久的? This way I dont have to worry about losing data if my server crashes. 这样,如果我的服务器崩溃,我不必担心丢失数据。

  2. I can't seem to control the name of the queue. 我似乎无法控制队列的名称。 When I run rabbitmqctl.bat list_queues I see that the queue is called amq.gen-3IgZD30XvTEQWNRsezSUUA== . 当我运行rabbitmqctl.bat list_queues我看到队列名为amq.gen-3IgZD30XvTEQWNRsezSUUA== Is there a way to control the name of the queue? 有没有办法控制队列的名称?

That cannot be done with the WCF bindings. 使用WCF绑定无法做到这一点。 See this mailing list thread for details. 请参阅邮件列表主题以获取详细信

Basically, you cannot control the queue name via WCF, which means you are limited to anonymous queues (like the one you are seeing), which, in turn, means that you can only use non-persistent queues. 基本上,您无法通过WCF控制队列名称,这意味着您只能使用匿名队列(如您所见的队列),这反过来意味着您只能使用非持久队列。

If you need more control than is offered by the WCF bindings, you should consider using the full .NET client. 如果您需要比WCF绑定提供的更多控制,则应考虑使用完整的.NET客户端。 It's quite easy to use, and there are a bunch of tutorials to help you get started (they are in Java, but the .NET API is very similar). 它非常易于使用,并且有很多教程可以帮助您入门(它们使用的是Java,但.NET API非常相似)。

I had the same problem as you did and what I did was to edit the source code of the rabbitMQDotNetClient. 我遇到了和你一样的问题,我做的是编辑rabbitMQDotNetClient的源代码。

File: RabbitMQInputChannel.cs 文件: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();
    }

Compile with the flag USE_DEFINED_QUEUE_NAMES. 使用标志USE_DEFINED_QUEUE_NAMES进行编译。 This will create a queue name with the name you have given in your app.config or web.config file. 这将创建一个队列名称,其名称与app.config或web.config文件中的名称一致。 You can always change the queues options on the QueueDeclare(...) if you want your queues to behave differently than the ones I'm creating. 如果您希望队列的行为与我正在创建的队列不同,您可以随时更改QueueDeclare(...)上的队列选项。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM