How to detect that all subscribers to an event hav

2019-07-05 05:09发布

问题:

I am currently investigating Mass Transit. I have written a simple sample based on the Starbucks sample.

I have the following saga:

    Define(() =>
    {
        Initially(
            When(ReportRequest)
                .Then((saga, message) => saga.ProcessReportRequest(message))
                .TransitionTo(WaitingForReportToComplete)
            );

        During(WaitingForReportToComplete, When(ReportComplete)
            .Then((saga, message) =>
                {
                    Console.WriteLine("Report Complete for '{0}'", saga.Name);
                    saga.CompleteReportRequest(message);
                })
                .Complete()
            );
    });

The problem I am having is that I have X subscribers to the ReportRequest event and each subscriber create a small part of the report. Therefore, for the report to be considered complete all subscribers need to have issued a ReportComplete event. At the moment the first subscriber to complete issues a ReportComplete event and the saga then also completes.

How do I set up a saga so that it waits for all X subscribers to respond to a given message before completing?

回答1:

I'm thinking it's a two step solution:

  • first you need to "enlist" all the subscribers that you expect to participate. This could be a static/shared information, maintained outside of the saga, depending on your scenario.
  • only when you received "complete" from all enlisted participants you can move to Complete state. You can instruct the saga imperatively to move to another state.