Created
November 21, 2017 13:13
-
-
Save janpieterz/cb24b1840ba9639b31c49b1f6303172a to your computer and use it in GitHub Desktop.
NServiceBus RoundRobinFailoverPartitioningStrategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RoundRobinPartitioningFailoverStrategy : INamespacePartitioningStrategy | |
{ | |
private readonly CircularBuffer<RuntimeNamespaceInfo[]> _namespaces; | |
public RoundRobinPartitioningFailoverStrategy(ReadOnlySettings settings) | |
{ | |
if (!settings.TryGet("AzureServiceBus.Settings.Topology.Addressing.Namespaces", | |
out NamespaceConfigurations namespaces)) | |
{ | |
throw new ConfigurationErrorsException($"The '{nameof(RoundRobinPartitioningFailoverStrategy)}' strategy requires exactly two namespaces to be configured, please use {nameof(AzureServiceBusTransportExtensions.NamespacePartitioning)}().{nameof(AzureServiceBusNamespacePartitioningSettings.AddNamespace)}() to register the namespaces."); | |
} | |
var partitioningNamespaces = namespaces.Where(x => x.Purpose == NamespacePurpose.Partitioning).ToList(); | |
if (partitioningNamespaces.Count != 2) | |
{ | |
throw new ConfigurationErrorsException($"The '{nameof(RoundRobinPartitioningFailoverStrategy)}' strategy requires exactly two namespaces to be configured, please use {nameof(AzureServiceBusTransportExtensions.NamespacePartitioning)}().{nameof(AzureServiceBusNamespacePartitioningSettings.AddNamespace)}() to register the namespaces."); | |
} | |
_namespaces = new CircularBuffer<RuntimeNamespaceInfo[]>(partitioningNamespaces.Count); | |
var first = namespaces.First(); | |
var second = namespaces.Last(); | |
_namespaces.Put(new [] | |
{ | |
new RuntimeNamespaceInfo(first.Alias, first.ConnectionString, first.Purpose, NamespaceMode.Active), | |
new RuntimeNamespaceInfo(second.Alias, second.ConnectionString, second.Purpose, NamespaceMode.Passive), | |
}); | |
_namespaces.Put(new [] | |
{ | |
new RuntimeNamespaceInfo(first.Alias, first.ConnectionString, first.Purpose, NamespaceMode.Passive), | |
new RuntimeNamespaceInfo(second.Alias, second.ConnectionString, second.Purpose, NamespaceMode.Active), | |
}); | |
} | |
public IEnumerable<RuntimeNamespaceInfo> GetNamespaces(PartitioningIntent partitioningIntent) | |
{ | |
return _namespaces.Get(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment