开发者

Can the NServiceBus distributor report progress from workers?

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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜