Uutiset

Adafyn kuulumisia

Using Azure Service Bus Topics in dynamic scenarios

<p>This post shows how to use Azure Service Bus Topics and filters to handle a scenario where events and event handers aren’t known when starting the system. </p> <h2>Background</h2> <p>One of our systems can contain 0 or 10 different event types. And it can contain 0 or 100 event handlers. The idea is that the event handlers can be added dynamically runtime. And to make things even more interesting, also the events can be added runtime. One event can have zero-n event handlers. </p> <p>We use Azure Service Bus topics for the pub&amp;sub model of communication. </p> <h2>The problem</h2> <p>The problem is that if we don’t know the types of events when starting the system, how can we easily create the required topics?</p> <h2>The solution</h2> <p>The solution was to only use one pre-defined topic and then to filter the communications using Azure Service Bus subscription filters.</p> <h2>More details</h2> <p>As the events and event handlers can change dynamically when the system is running, pre-creating all the Service Bus topics is cumbersome and not actually possible. To get around this there’s couple options:</p> <ol> <li>The event creator and event handler both try to create the Service Bus Topic if it doesn’t exists. <li>All the event creators and handlers use the same pre-created topic and use message properties and subscription filters to handle only the relevant messages.</li></ol> <p>We ended up using the second option. So there’s only one topic (<em>system-events)<strong>&nbsp;</strong></em>and all the event creators push their messages into the same topic.</p> <p>When pushing the event, the event creator adds a property to message which defined the message’s type. For example <em>newinvoice</em>.</p> <p>All the event handlers then subscribe to the same <em>system-events </em>topic. But when creating the subscription, they attach a filter to the subscription, indicating what types of messages they are interested in. </p> <h2>How to use the topic filters in C#</h2> <p>The official Azure GitHub contains a <a href="https://github.com/Azure/azure-service-bus/tree/master/samples/DotNet/Microsoft.ServiceBus.Messaging/TopicFilters" target="_blank">good sample</a> of using Service Bus topic filters in C#.</p> <p>Main thing is to specify the filter on event handler when creating the subscription:<br><pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"> await namespaceManager.CreateSubscriptionAsync(
"system-events",
"newinvoicesubs"
new SqlFilter("eventtype = ’newinvoice’));</pre></p>
<p>Other thing to remember is to define the event type when pushing to the topic:</p><pre class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">var message = new BrokeredMessage();
message.Properties = {{ "eventtype", "newinvoice" }};
await topicClient.SendAsync(message);
</pre>