开发者

NServiceBus filtering messages to subscribers

I'm just getting started with NService bus and I want to know how to filter message when I send them so that they only go to particular subscribers.

For example, lets say I have a database with products that are categorized. My publisher will check the database every N seconds and will send messages when a new product is added to the database.

But each subscriber is only interested in a particular category and assuming that I can get the subscriber to send the category they are interested in, I then would like to only publish messages about products in a particular category to subscribers that are interested in them.

The categories are dynamic, so I can't create different messages for the different categories. So for that reason I assume that all the subscribers have to subscribe to the same published IMessage.

EDIT: To clear up some confusion, here's another example that's closer to the real thing.

My publisher's job is to notify subscribers about new posts on stackoverflow based on tags. But I don't want the publisher to be querying stackoverflow for tags that no-one is interested in... not to mention to overhead of doing something like that.

So when a subscriber subscribes, they register their interest along with some metadata telling the publisher what posts they are interested in - 开发者_运维百科e.g. posts tagged NServiceBus.

Now the publisher knows that they have to monitor stackoverflow for new posts tagged NServiceBus and can start doing just that.

So when there is a new post tagged with NServiceBus and my publisher goes to notify it's subscribers, I want it to only notify the subscribers that are interested in that tag... and not subscribers that are interested in different tags.

Make more sense?

I'm just getting started on this project, so if I'm going down the wrong road I'd appreciate a heads up and suggestions to use a different set of tools.


Assuming you get the categorization subscriptions to work, it should be sufficient to broadcast the category and associated products and have each endpoint just ignore the products it doesn't care about. Otherwise you'd have to create a message per product and include the category so endpoint can ignore configured categories.


The 'PubSub' example that comes with NServiceBus demonstrates this scenario.

The messages consist of the following:

using NServiceBus;
using System;

namespace MyMessages
{
    [Serializable]
    public class EventMessage : IEvent
    {
        public Guid EventId { get; set; }
        public DateTime? Time { get; set; }
        public TimeSpan Duration { get; set; }
    }

    public interface IEvent : IMessage
    {
        Guid EventId { get; set; }
        DateTime? Time { get; set; }
        TimeSpan Duration { get; set; }
    }
}

Then the publisher (in the example) alternates making either a EventMessage or an IEvent, and publishing that.

var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

eventMessage.EventId = Guid.NewGuid();
eventMessage.Time = DateTime.Now.Second > 30 ? (DateTime?)DateTime.Now : null;
eventMessage.Duration = TimeSpan.FromSeconds(99999D);

Bus.Publish(eventMessage);

Subscriber 1 handles messages of type:

namespace Subscriber1
{
    public class EventMessageHandler : IHandleMessages<EventMessage>

While Subscriber 2 handles the generic IEvent (which will result in handling ALL messages, EventMessage and IEvent)

namespace Subscriber2
{
    public class EventMessageHandler : IHandleMessages<IEvent>

This should help you get in the right direction for handling different product categories by different subscribers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜