Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#686: Move ConsumerGroup management functions to separate class #826

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 17 additions & 96 deletions src/Common/Helpers/ServiceBusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,11 @@ public class ServiceBusHelper
private const string EventHubDescriptionCannotBeNull = "The event hub description argument cannot be null.";
private const string ConsumerGroupCannotBeNull = "The consumerGroup argument cannot be null or empty.";
private const string PartitionDescriptionCannotBeNull = "The partition description argument cannot be null.";
private const string ConsumerGroupDescriptionCannotBeNull = "The consumer group description argument cannot be null.";
private const string PathCannotBeNull = "The path argument cannot be null or empty.";
private const string NameCannotBeNull = "The name argument cannot be null or empty.";
private const string DescriptionCannotBeNull = "The description argument cannot be null.";
private const string ServiceBusIsDisconnected = "The application is now disconnected from any service bus namespace.";
private const string ServiceBusIsConnected = "The application is now connected to the {0} service bus namespace.";
private const string ConsumerGroupCreated = "The consumer group {0} has been successfully created.";
private const string ConsumerGroupDeleted = "The consumer group {0} has been successfully deleted.";
private const string ConsumerGroupUpdated = "The consumer group {0} has been successfully updated.";
private const string WarningHeader = "The following validations failed:";
private const string WarningFormat = "\n\r - {0}";
private const string PropertyConversionError = "{0} property conversion error: {1}";
Expand Down Expand Up @@ -180,6 +176,7 @@ public class ServiceBusHelper
private IServiceBusRelay serviceBusRelay;
private IServiceBusNotificationHub serviceBusNotificationHub;
private IServiceBusEventHub serviceBusEventHub;
private IServiceBusConsumerGroup serviceBusConsumerGroup;
#endregion

#region Private Static Fields
Expand Down Expand Up @@ -246,6 +243,7 @@ public ServiceBusHelper(WriteToLogDelegate writeToLog, ServiceBusHelper serviceB
serviceBusRelay = serviceBusHelper.serviceBusRelay;
serviceBusNotificationHub = serviceBusHelper.serviceBusNotificationHub;
serviceBusEventHub = serviceBusHelper.serviceBusEventHub;
serviceBusConsumerGroup = serviceBusHelper.serviceBusConsumerGroup;
}
#endregion

Expand Down Expand Up @@ -365,6 +363,11 @@ public string Scheme
{
serviceBusEventHub.Scheme = scheme;
}

if (serviceBusConsumerGroup != null)
{
serviceBusConsumerGroup.Scheme = scheme;
}
}
}
}
Expand Down Expand Up @@ -803,6 +806,7 @@ public bool Connect(ServiceBusNamespace serviceBusNamespace)
serviceBusRelay = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusRelay(sbn, nsmgr));
serviceBusNotificationHub = CreateServiceBusEntity((sbn, nsmgr) => new ServiceBusNotificationHub(sbn, nsmgr, notificationHubNamespaceManager));
serviceBusEventHub = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusEventHub(sbn, nsmgr));
serviceBusConsumerGroup = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusConsumerGroup(sbn, nsmgr));

WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ServiceBusIsConnected, namespaceManager.Address.AbsoluteUri));
namespaceUri = namespaceManager.Address;
Expand Down Expand Up @@ -1106,19 +1110,7 @@ public Uri GetPartitionUri(string eventHubName, string consumerGroupName, string
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of consumer groups attached to the event hub passed as a parameter.</returns>
public ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string name)
{
if (string.IsNullOrWhiteSpace(eventHubPath))
{
throw new ArgumentException(PathCannotBeNull);
}
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(NameCannotBeNull);
}
if (namespaceManager != null)
{
return RetryHelper.RetryFunc(() => namespaceManager.GetConsumerGroup(eventHubPath, name), writeToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusConsumerGroup.GetConsumerGroup(eventHubPath, name);
}

/// <summary>
Expand All @@ -1128,15 +1120,7 @@ public ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string nam
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of consumer groups attached to the event hub passed as a parameter.</returns>
public IEnumerable<ConsumerGroupDescription> GetConsumerGroups(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (namespaceManager != null)
{
return RetryHelper.RetryFunc(() => namespaceManager.GetConsumerGroups(path), writeToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusConsumerGroup.GetConsumerGroups(path);
}

/// <summary>
Expand All @@ -1146,15 +1130,7 @@ public IEnumerable<ConsumerGroupDescription> GetConsumerGroups(string path)
/// <returns>Returns an IEnumerable<SubscriptionDescription/> collection of consumer groups attached to the event hub passed as a parameter.</returns>
public IEnumerable<ConsumerGroupDescription> GetConsumerGroups(EventHubDescription description)
{
if (description == null)
{
throw new ArgumentException(EventHubDescriptionCannotBeNull);
}
if (namespaceManager != null)
{
return RetryHelper.RetryFunc(() => namespaceManager.GetConsumerGroups(description.Path), writeToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusConsumerGroup.GetConsumerGroups(description);
}

/// <summary>
Expand All @@ -1164,18 +1140,7 @@ public IEnumerable<ConsumerGroupDescription> GetConsumerGroups(EventHubDescripti
/// <returns>Returns a newly-created ConsumerGroupDescription object.</returns>
public ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription description)
{
if (description == null)
{
throw new ArgumentException(DescriptionCannotBeNull);
}
if (namespaceManager != null)
{
var consumerGroup = RetryHelper.RetryFunc(() => namespaceManager.CreateConsumerGroup(description), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupCreated, description.Name));
OnCreate?.Invoke(new ServiceBusHelperEventArgs(consumerGroup, EntityType.ConsumerGroup));
return consumerGroup;
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusConsumerGroup.CreateConsumerGroup(description);
}

/// <summary>
Expand All @@ -1185,20 +1150,7 @@ public ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription des
/// <param name="name">Name of the consumer group.</param>
public void DeleteConsumerGroup(string eventHubName, string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(PathCannotBeNull);
}
if (namespaceManager != null)
{
RetryHelper.RetryAction(() => namespaceManager.DeleteConsumerGroup(eventHubName, name), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupDeleted, name));
OnDelete?.Invoke(new ServiceBusHelperEventArgs(new ConsumerGroupDescription(eventHubName, name), EntityType.ConsumerGroup));
}
else
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
serviceBusConsumerGroup.DeleteConsumerGroup(eventHubName, name);
}

/// <summary>
Expand All @@ -1207,20 +1159,7 @@ public void DeleteConsumerGroup(string eventHubName, string name)
/// <param name="consumerGroupDescription">The consumer group to delete.</param>
public void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescription)
{
if (string.IsNullOrWhiteSpace(consumerGroupDescription?.Name))
{
throw new ArgumentException(ConsumerGroupDescriptionCannotBeNull);
}
if (namespaceManager != null)
{
RetryHelper.RetryAction(() => namespaceManager.DeleteConsumerGroup(consumerGroupDescription.EventHubPath, consumerGroupDescription.Name), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupDeleted, consumerGroupDescription.Name));
OnDelete?.Invoke(new ServiceBusHelperEventArgs(consumerGroupDescription, EntityType.ConsumerGroup));
}
else
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
serviceBusConsumerGroup.DeleteConsumerGroup(consumerGroupDescription);
}

/// <summary>
Expand All @@ -1230,14 +1169,7 @@ public void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescriptio
/// </summary>
public void DeleteConsumerGroups(string eventHubName, IEnumerable<string> consumerGroups)
{
if (consumerGroups == null)
{
return;
}
foreach (var consumerGroup in consumerGroups)
{
DeleteConsumerGroup(eventHubName, consumerGroup);
}
serviceBusConsumerGroup.DeleteConsumerGroups(eventHubName, consumerGroups);
}

/// <summary>
Expand All @@ -1247,18 +1179,7 @@ public void DeleteConsumerGroups(string eventHubName, IEnumerable<string> consum
/// <returns>Returns an updated ConsumerGroupDescription object.</returns>
public ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription description)
{
if (description == null)
{
throw new ArgumentException(DescriptionCannotBeNull);
}
if (namespaceManager != null)
{
var consumerGroup = RetryHelper.RetryFunc(() => namespaceManager.UpdateConsumerGroup(description), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ConsumerGroupUpdated, description.Name));
OnCreate?.Invoke(new ServiceBusHelperEventArgs(consumerGroup, EntityType.ConsumerGroup));
return consumerGroup;
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusConsumerGroup.UpdateConsumerGroup(description);
}

/// <summary>
Expand All @@ -1269,7 +1190,7 @@ public ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription des
/// <returns>The absolute uri of the consumer group.</returns>
public Uri GetConsumerGroupUri(string eventHubName, string consumerGroupPath)
{
return Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri(scheme, Namespace, string.Concat(ServicePath, eventHubName, "/", consumerGroupPath));
return serviceBusConsumerGroup.GetConsumerGroupUri(eventHubName, consumerGroupPath);
}

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions src/Common/WindowsAzure/IServiceBusConsumerGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusExplorer.WindowsAzure
{
internal interface IServiceBusConsumerGroup : IServiceBusEntity
{
ConsumerGroupDescription CreateConsumerGroup(ConsumerGroupDescription description);

void DeleteConsumerGroup(ConsumerGroupDescription consumerGroupDescription);

void DeleteConsumerGroup(string eventHubName, string name);

void DeleteConsumerGroups(string eventHubName, IEnumerable<string> consumerGroups);

ConsumerGroupDescription GetConsumerGroup(string eventHubPath, string name);

IEnumerable<ConsumerGroupDescription> GetConsumerGroups(EventHubDescription description);

IEnumerable<ConsumerGroupDescription> GetConsumerGroups(string path);

Uri GetConsumerGroupUri(string eventHubName, string consumerGroupPath);

ConsumerGroupDescription UpdateConsumerGroup(ConsumerGroupDescription description);
}
}
Loading
Loading