简体   繁体   中英

How to send a message to only one Azure Service Bus Subscription?

I have the following scenario: I have 2 microservices subscribes in an Azure Service Bus, one microservice to send push notifications and another to send emails, these microservice will never work together, so the message will execute only by one microservice.

I was searching and looks like a good idea to send the message to a Topic and have a filter to choose what microservice should execute the operation.

So I have two questions:

Should I use Topics and filters? Not sure if's the better practices.

If yes, exist a way to send the message to the correct subscription? For example, I publish a message to subscription X instead of publishing for all subscriptions.

在此处输入图像描述

Thanks

Should I use Topics and filters? Not sure if's the better practices.

Yes. This is a classic use case to topics and subscriptions.

If yes, exist a way to send the message to the correct subscription? For example, I publish a message to subscription X instead of publishing for all subscriptions.

The way publishing works is that you publish it to the topic, the topic takes the messages and validates against the filters on all the subscriptions. Subscriptions with the satisfying filter criteria receive the message. Subscriptions that do not have filter criteria satisfied are skipped. There are three types of filters, Boolean, SQL and Correlation. Boolean filters are less relevant to you. Correlation or SQL filters will do the job. You can find more info on filters in my post .

What I would suggest here is to "stamp" each message with the method of notification. Since you're going to use a single notification method per method, the Correlation filter would be the simplest and most efficient. Notification method value could be assigned to the Label system property of the outgoing messages and a correlation filter on both subscriptions (one for email service and one for push notification services). For example (pseudo code):

var message = new Message();

// custom logic to determine what notification method to use

if (notificationMethod == NotificationMethod.Email)
{
  message.Label = "email";
}
else
{
  message.Label = "push";
}

// publish the message to the topic

And the two filters setup would be:

// email notification service subscription
var emailFilter = new CorrelationFilter();
filter.Label = "email";

// push notifications service subscription
var pushFilter = new new CorrelationFilter();
filter.Label = "push";

Creating subscription with a given filter (email for example):

var management = new ManagementClient(...);
await management.CreateSubscriptionAsync(new SubscriptionDescription("topic", "subscription"));

var client = new SubscriptionClient("topic", "subscription");
await client.AddRuleAsync(new RuleDescription(
{
  Filter = emailFilter,
  Name = "email filter"
}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM