Skip to content

Commit

Permalink
Merge pull request #74 from wemogy/fix/improve-scheduled-command-serv…
Browse files Browse the repository at this point in the history
…ice-implementation

Improved AzureServiceBusScheduledCommandService with ConcurrentDictio…
  • Loading branch information
SebastianKuesters authored Jan 9, 2024
2 parents fbad959 + 4a40066 commit dd8bc57
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Wemogy.Core.Errors;
Expand All @@ -16,23 +16,21 @@ public class AzureServiceBusScheduledCommandService : IScheduledCommandService
private const string ImmediateMessageJobId = "IMMEDIATE_MESSAGE";
private readonly ServiceBusClient _serviceBusClient;
private readonly AzureServiceBusSetupEnvironment _azureServiceBusSetupEnvironment;
private readonly Dictionary<string, ServiceBusSender> _serviceBusSenders;
private readonly ConcurrentDictionary<string, ServiceBusSender> _serviceBusSenders;

public AzureServiceBusScheduledCommandService(ServiceBusClient serviceBusClient, AzureServiceBusSetupEnvironment azureServiceBusSetupEnvironment)
{
_serviceBusClient = serviceBusClient;
_azureServiceBusSetupEnvironment = azureServiceBusSetupEnvironment;
_serviceBusSenders = new Dictionary<string, ServiceBusSender>();
_serviceBusSenders = new ConcurrentDictionary<string, ServiceBusSender>();
}

private ServiceBusSender GetServiceBusSender<TCommand>()
{
var queueOrTopicName = _azureServiceBusSetupEnvironment.GetQueueName<TCommand>();
if (!_serviceBusSenders.TryGetValue(queueOrTopicName, out var serviceBusSender))
{
serviceBusSender = _serviceBusClient.CreateSender(queueOrTopicName);
_serviceBusSenders.TryAdd(queueOrTopicName, serviceBusSender);
}
var serviceBusSender = _serviceBusSenders.GetOrAdd(
queueOrTopicName,
_ => _serviceBusClient.CreateSender(queueOrTopicName));

return serviceBusSender;
}
Expand Down

0 comments on commit dd8bc57

Please sign in to comment.