0
votes

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.

2
@V4Vendetta - I also asked that other question and, albeit related, it's completely different.Charlino
The issue with the functionality you want from NServiceBus here is that it is broker-related - it deals with data distribution where there is logical coupling between "publisher" and "subscriber". Can you explain what the relationship is between various subscribers - what differentiates them from each other?Udi Dahan
@Udi Dahan: Using my 2nd example as a base, lets say the various things that differentiates the subscribers are 1) What they are interested in, e.g. new posts tagged NServiceBus or new posts tagged ASP.NET MVC 2) How often they want to be notified of new posts, e.g. every 10 minutes or every 3 hours.Charlino
@Charlino Technologically, or from a deployment perspective, what is the difference between these subscribers?Udi Dahan

2 Answers

1
votes

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.

0
votes

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.