您可以在 RabbitMQ .Net 客户端中处理队列时发布消息吗?

发布于 2024-09-27 20:25:18 字数 807 浏览 1 评论 0原文

我有几个消息传递场景,需要在 c# 中使用 RabbitMQ 2.1.0 的帮助... 1)我想让订阅者监听“原始”队列;然后做一些 预处理并发布新消息,例如“预处理”到相同的消息 交换。 2) 与 1 类似,但发布到不同的交易所,

我在 .Net 客户端用户指南中注意到它说不要调用 .basicPublish 在回调期间阻塞线程。

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);
        }

    }
}

我想在我之前成功处理并发布新消息 发送原始消息的ack();只是为了让我确信每条消息都是 已处理。

这是正确的处理方式还是会导致线程问题?

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-10-04 20:25:18

您所说的关于回调的内容是正确的,但它仅适用于您子类化 DefaultBasicConsumer (或从头开始编写自己的消费者)。

在您的情况下,订阅是消费者,它本身不应该导致任何锁定。您拥有的代码很好,因为它仅使用订阅使用者。那时 BasicPublish 应该是安全的。

您的代码也恰好(或多或少)是我们的 订阅者示例。

另外,很抱歉没有在rabbitmq-discuss 上回复。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文