Can you publish a message while processing a queue

2019-08-18 13:41发布

I have a couple messaging scenarios I need help with using RabbitMQ 2.1.0 in c#... 1) I would like to have a subscriber listening to "raw" queue; then do some preprocessing and publish a new message, such as "preprocessed" to the same exchange. 2) similar to 1 but publish to a different exchange

I noticed in the .Net Client User Guide that it says do not call .basicPublish during a callback as it blocks threads.

using (IConnection conn = connectionFactory.CreateConnection())
{
    using (IModel model = conn.CreateModel())
    {
        var sub = new Subscription(model, "rtls");
        foreach (BasicDeliverEventArgs iter in sub)
        {
            var message = System.Text.Encoding.UTF8.GetString(iter.Body);
            //do stuff and build up a new message
            //possibly create a new connection?
            //  ***.BasicPublish(new message);

            sub.Ack(iter);
        }

    }
}

I would like to successfully process and publish the new message before I send the ack() on the original message; just so I'm sure every message is processed.

Is this the proper way to process or will it cause threading issues?

Thank you for your help!

1条回答
在下西门庆
2楼-- · 2019-08-18 14:03

What you say about callbacks is true, but it only applies if you're subclassing DefaultBasicConsumer (or writing your own consumer from scratch).

In your case, Subscription is the consumer and it shouldn't cause any locks by itself. The code you have is fine, since it only uses the Subscription consumer. It should be safe to BasicPublish at that point.

Your code also happens to be (more or less) our Subscriber example.

Also, sorry for not answering on rabbitmq-discuss.

查看更多
登录 后发表回答