Rebus subscriber-publisher system. Process message

2019-08-15 05:12发布

问题:

I have system with one publisher several subscribers. But some messages should be processed only by single subscriber. In my case publisher sends message about changing data in database, all subscribers has access to the same database, but I don't need them all change the same data. How can this be accomplished using rebus?

PS. Forgot to mention. I can't subscribe to the message only with one subscriber, because subscriberss can go online/offline all the time.

回答1:

But some messages should be processed only by single subscriber

Then you should not use bus.Publish for that particular message type - there is a mechanism that sends messages to one particular recipient which you should use - you do it by

  1. mapping a message type to an endpoint
  2. "sending" the message instead of "publishing" it

You do (1) like this:

Configure.With(...)
    .(...)
    .Routing(r => {
        r.TypeBased()
            .Map<YourMessage>("the_recipient");
    })
    .Start();

thus telling Rebus that whoever gets its messages from the queue the_recipient is the owner of messages of type YourMessage and should be considered the natural recipient for an implicitly routed message of that type.

You do (2) like this:

await bus.Send(new YourMessage(...));

and then Rebus will send the message to the message's natural owner.

I hope that does the trick for you :)