NServiceBus 经销商可以报告工人的进度吗?

发布于 2024-10-14 08:42:49 字数 972 浏览 7 评论 0原文

我正在调查 NServiceBus,我不确定如何(甚至是否)可以使用它来处理这种情况:

我有多个客户端发送工作请求,分销商将其外包给工人。这项工作需要很长时间才能完成,我希望工作人员向发送原始请求的客户报告进度。

我查看了全双工示例以及如何将分配器添加到该示例。我已经让这些工作正常,但是当我修改它们以回复一系列进度消息(消息之间有延迟,如下所示的代码)时,客户端会同时收到所有进度消息。

public class RequestDataMessageHandler : IHandleMessages<RequestDataMessage>
{
    public IBus Bus { get; set; }

    public void Handle(RequestDataMessage message)
    {
        for (var i = 0; i < 10; i++)
        {
            var count = i;
            var response = this.Bus.CreateInstance<DataResponseMessage>(m =>
                {
                    m.DataId = message.DataId;
                    m.Progress = count * 10;
                });

            this.Bus.Reply(response);

            Thread.Sleep(1000);
        }
    }
}

我怀疑我还没有理解 NServiceBus 工作原理的一些基本知识。有人可以解释我哪里出了问题,或者给我指出一些示例和/或文档吗?

I am investigating NServiceBus and I am unsure how (or even if) I could use it to handle this scenario:

I have multiple clients sending work requests, which the distributor farms out to workers. The work will take a long time to complete and I would like the workers to report progress back to the client that sent the original request.

I have looked at the full duplex sample and also how to add the distributor to that sample. I've got these working, but when I modify them to reply with a series of progress messages (with a delay between the messages, as per code shown below), the client receives all the progress messages at the same time.

public class RequestDataMessageHandler : IHandleMessages<RequestDataMessage>
{
    public IBus Bus { get; set; }

    public void Handle(RequestDataMessage message)
    {
        for (var i = 0; i < 10; i++)
        {
            var count = i;
            var response = this.Bus.CreateInstance<DataResponseMessage>(m =>
                {
                    m.DataId = message.DataId;
                    m.Progress = count * 10;
                });

            this.Bus.Reply(response);

            Thread.Sleep(1000);
        }
    }
}

I suspect I've not understood something basic about how NServiceBus works. Could someone explain where I've gone wrong, or point me at some examples and/or documentation?

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

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

发布评论

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

评论(2

倾城泪 2024-10-21 08:42:49

您构建的内容将始终将消息作为同一事务的一部分发送。由于每个处理程序有一个事务,因此您将无法以这种方式传达进度。您必须为每个处理块拥有一个单独的端点来传达进度。我们通过更新事务中未涉及的外部内容来实现进度沟通。这可以通过向另一个端点发送非事务性消息来更新进度或类似 RPC 调用的方式来完成。从那里你可以进行一些轮询来推进数据存储。

What you have constructed will always send the messages as part of the same transaction. Since there is one transaction per handler, you won't be able to communicate progress this way. You would have to have a separate endpoint for each chunk of processing that would communicate progress. We've implemented communicating progress by updating something externally that is not involved in the transaction. That could be done by sending a non-transactional message to another endpoint to update progress or something like an RPC call. From there you could have something poll that progress data store.

梦太阳 2024-10-21 08:42:49

让您的工作人员使用bus.Reply() 将消息发送回您的客户。回复会自动将消息发送到发送原始消息的端点

Have your workers use bus.Reply() to send messages back to your clients. Reply will automatically send the message to the endpoint that sent the original message

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