Skip to content

Commit

Permalink
Add functionality for RabbitMQ connection string management options
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisNickels committed Feb 2, 2025
1 parent ac854a1 commit 162d345
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace ServiceControl.Transports.RabbitMQ
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerThroughput;
using Microsoft.Extensions.DependencyInjection;
using NServiceBus;
Expand All @@ -22,8 +24,17 @@ protected override RabbitMQTransport CreateTransport(TransportSettings transport
throw new InvalidOperationException("Connection string not configured");
}

var connectionStringDictionary = ConnectionConfiguration.ParseNServiceBusConnectionString(transportSettings.ConnectionString, new StringBuilder());
var disableManagementApi = GetValue(connectionStringDictionary, "DisableManagementApi", "false");
if (!disableManagementApi.Equals("true", StringComparison.OrdinalIgnoreCase) && !disableManagementApi.Equals("false", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException("The value for 'DisableManagementApi' must be either 'true' or 'false'");
}

var transport = new RabbitMQTransport(RoutingTopology.Conventional(queueType), transportSettings.ConnectionString, enableDelayedDelivery: false);
transport.TransportTransactionMode = transport.GetSupportedTransactionModes().Contains(preferredTransactionMode) ? preferredTransactionMode : TransportTransactionMode.ReceiveOnly;
transport.ManagementApiUrl = GetValue(connectionStringDictionary, "ManagementApiUrl", string.Empty);
transport.UseManagementApi = disableManagementApi.Equals("false", StringComparison.OrdinalIgnoreCase);

return transport;
}
Expand All @@ -39,5 +50,10 @@ protected sealed override void AddTransportForMonitoringCore(IServiceCollection
services.AddSingleton<IProvideQueueLength, QueueLengthProvider>();
services.AddHostedService(provider => provider.GetRequiredService<IProvideQueueLength>());
}

static string GetValue(Dictionary<string, string> dictionary, string key, string defaultValue)
{
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
}
}
}

0 comments on commit 162d345

Please sign in to comment.