Distributed Event Broker – Custom messages, selection strategies and restrictions

In my last post I gave an overview about some of the customization possibilities of the distributed event broker. In this post we are going even deeper into some of the customization possibilities the distributed event broker offers. I want to cover the following topics: Custom messages and selection strategies. Furthermore I want to give some details about the current restrictions of the distributed event broker.

Custom Message

If you want to extend the standard message with additional information or if your transport layer has restrictions on the message contracts, you have to inherit from DefaultDistributedFactory and override CreateMessageFactory.

Your message factory can extend AbstractEventMessageFactory for simple scenarios.

Example (taken from NServiceBus transport):

    // Custom message interface
    public interface INServiceBusEventFired : IEventFired, IMessage
    {
    }
    // Custom message implementation
    public class NServiceBusEventFired : EventFired, INServiceBusEventFired
    {
    }
    // Custom message factory
    public class NServiceBusEventMessageFactory : AbstractEventMessageFactory
    {
        public override IEventFired CreateEventFiredMessage(Action<IEventFired> initializer)
        {
            return this.CreateEventFiredMessage<NServiceBusEventFired, IEventFired>(initializer);
        }
    }
    // Custom distributed factory
    public class NServiceBusDistributedFactory : DefaultDistributedFactory
    {
        public override IEventMessageFactory CreateMessageFactory()
        {
            return new NServiceBusEventMessageFactory();
        }
    }

Selection Strategy

Selection strategies are queried when a new topic is created on the event broker where the extension is added. The default selection strategy accepts all topics. This means that all events fired on the event broker which has the extension will be published on the transport layer.

If you want to use your own convention you have to inherit from DefaultDistributedFactory and override CreateTopicSelectionStrategy. For simple scenarios the FuncTopicSelectionStrategy can be used which accepts a lambda which acts as strategy implementation.

Example:

    public class CustomDistributedFactory: DefaultDistributedFactory     {
        public override ITopicSelectionStrategy CreateTopicSelectionStrategy()
        {
            return new FuncTopicSelectionStrategy(topic => topic.Uri.StartsWith("distributed://", StringComparison.Ordinal);
        }
    }

The strategy above would only accept event publications where the topic URI starts with distributed://. All other publications would not be published on the transport layer.

Restrictions

Synchronous subscription of events which are fired on the distributed event broker is not allowed. In my point of view this makes no sense and would couple the distributed event broker too tight to its participants in the network. Therefore all publications must use asynchronous handler restrictions.

About the author

Daniel Marbach

Add comment

By Daniel Marbach

Recent Posts