NServiceBus 过滤发送给订阅者的消息

发布于 2024-11-18 10:41:14 字数 929 浏览 2 评论 0原文

我刚刚开始使用 NService 总线,我想知道如何在发送消息时过滤消息,以便它们只发送给特定的订阅者。

例如,假设我有一个包含已分类产品的数据库。我的发布者将每 N 秒检查一次数据库,并在将新产品添加到数据库时发送消息。

但每个订阅者只对特定类别感兴趣,并假设 我可以获得订阅者发送他们感兴趣的类别,然后我只想向对其感兴趣的订阅者发布有关特定类别中的产品的消息。

类别是动态的,因此我无法为不同的类别创建不同的消息。因此,出于这个原因,我假设所有订阅者都必须订阅相同的已发布的 IMessage

编辑:为了消除一些混乱,这里有另一个更接近真实情况的示例。

我的发布者的工作是根据标签通知订阅者有关 stackoverflow 上的新帖子的信息。但我不希望发布者在 stackoverflow 上查询没人感兴趣的标签……更不用说做类似事情的开销了。

因此,当订阅者订阅时,他们会注册自己的兴趣以及一些元数据,告诉发布者他们对哪些帖子感兴趣 - 例如标记为 NServiceBus 的帖子。

现在,发布者知道他们必须监视 stackoverflow 中是否有标记为 NServiceBus 的新帖子,并且可以开始这样做。

因此,当有一个带有 NServiceBus 标签的新帖子并且我的发布者去通知它的订阅者时,我希望它只通知对该标签感兴趣的订阅者......而不是对以下内容感兴趣的订阅者:不同的标签。

更有意义吗?

我刚刚开始这个项目,所以如果我走错了路,我会很感激您的提醒和建议,以使用不同的工具集。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霊感 2024-11-25 10:41:14

假设您使分类订阅正常工作,那么广播类别和关联产品就足够了,并且让每个端点忽略它不关心的产品。否则,您必须为每个产品创建一条消息并包含类别,以便端点可以忽略配置的类别。

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.

﹏半生如梦愿梦如真 2024-11-25 10:41:14

NServiceBus 附带的“PubSub”示例演示了这种情况。

消息由以下内容组成:

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; }
    }
}

然后发布者(在示例中)交替创建 EventMessage 或 IEvent,然后发布它。

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);

订阅者 1 处理以下类型的消息:

namespace Subscriber1
{
    public class EventMessageHandler : IHandleMessages<EventMessage>

而订阅者 2 处理通用 IEvent(这将导致处理所有消息、EventMessage 和 IEvent),

namespace Subscriber2
{
    public class EventMessageHandler : IHandleMessages<IEvent>

这应该可以帮助您找到正确的方向,通过不同的方式处理不同的产品类别。订户。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文