0

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.

Community
  • 1
  • 1
Charlino
  • 15,802
  • 3
  • 58
  • 74
  • possible duplicate of [NServiceBus sending information when a subscriber subscribes](http://stackoverflow.com/questions/6592499/nservicebus-sending-information-when-a-subscriber-subscribes) – V4Vendetta Jul 06 '11 at 06:53
  • @V4Vendetta - I also asked that other question and, albeit related, it's completely different. – Charlino Jul 06 '11 at 15:55
  • 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 Jul 10 '11 at 08:38
  • @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 Jul 11 '11 at 19:47
  • @Charlino Technologically, or from a deployment perspective, what is the difference between these subscribers? – Udi Dahan Jul 18 '11 at 11:16
  • @Udi Dahan: Technologically - they are just different instances of the same application. Deployment - all the instances would be deployed on various machines on an internal network. Thanks :-) – Charlino Jul 18 '11 at 16:50
  • Are you dealing with a latency sensitive domain and that's why you're having these user-facing apps subscribing to these events rather than just reading off of some server-side cache? – Udi Dahan Jul 20 '11 at 19:45

2 Answers2

1

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.

Adam Fyles
  • 6,030
  • 1
  • 23
  • 29
  • Thanks. I had thought that would be one way, but I am after a more complete solution would send messages to only the subscribers that are interested in it. – Charlino Jul 06 '11 at 15:49
0

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.

Phill
  • 18,398
  • 7
  • 62
  • 102
  • I understand how the PubSub example works... but the problem is that all the subscribers will have to subscribe to the same Message because the categories are dynamic and can be anything. – Charlino Jul 06 '11 at 15:46
  • How are they dynamic? That just sounds like you're over-complicating your system. – Phill Jul 06 '11 at 21:09
  • They are dynamic because I don't know what category they are interested in and the category could be anything... the subscriber could even make up a totally new category never seen before! So basically they all have to respond to the same message... unless you can think of another way around it. And yeah, the problem is actually a lot more complicated than the example I gave... I just gave the example to simplify things. – Charlino Jul 06 '11 at 21:33
  • A subscriber wouldn't 'make up a totally new category'. The publisher would. The publisher is the one who owns the category. It's the subscriber who might 'care' about the category. If you were shipping products, subscriber A might only care about shipping Frozen Food, while subscriber B might only care about Eletronic Goods. Since both are shipping those products differently. – Phill Jul 06 '11 at 21:49
  • The problem is actually a lot more complicated than the example I gave. Please see my edit for a more detailed example... I'm beginning to think I might be going about it the wrong way. Your thoughts would be appreciated. – Charlino Jul 06 '11 at 22:16
  • Hmmm it sounds like more of a custom RSS feed rather than something you would use NServiceBus for. You could implement a handler to be run first which checks the headers for specific tags it is interested in. Then attach the tags to the header before publishing it. Each subscriber will just discard the message if the tag(s) it's interested in are not in the header. – Phill Jul 06 '11 at 22:41
  • Udi Dahan has already answered that in your previous question also. – Phill Jul 06 '11 at 22:42