Skip to content

Commit

Permalink
Move EventHub management functions to separate class (#824)
Browse files Browse the repository at this point in the history
Co-authored-by: supereddie <>
  • Loading branch information
supereddie authored Jan 28, 2025
1 parent 5c7635b commit f555ed9
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 90 deletions.
116 changes: 26 additions & 90 deletions src/Common/Helpers/ServiceBusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ public class ServiceBusHelper
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 EventHubCreated = "The event hub {0} has been successfully created.";
private const string EventHubDeleted = "The event hub {0} has been successfully deleted.";
private const string EventHubUpdated = "The event hub {0} has been successfully updated.";
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.";
Expand Down Expand Up @@ -182,6 +179,7 @@ public class ServiceBusHelper
private IServiceBusRule serviceBusRule;
private IServiceBusRelay serviceBusRelay;
private IServiceBusNotificationHub serviceBusNotificationHub;
private IServiceBusEventHub serviceBusEventHub;
#endregion

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

Expand Down Expand Up @@ -351,6 +350,21 @@ public string Scheme
{
serviceBusRule.Scheme = scheme;
}

if (serviceBusRelay != null)
{
serviceBusRelay.Scheme = scheme;
}

if (serviceBusNotificationHub != null)
{
serviceBusNotificationHub.Scheme = scheme;
}

if (serviceBusEventHub != null)
{
serviceBusEventHub.Scheme = scheme;
}
}
}
}
Expand Down Expand Up @@ -788,6 +802,7 @@ public bool Connect(ServiceBusNamespace serviceBusNamespace)
serviceBusRule = CreateServiceBusEntity(static (sbn, nsmgr) => new ServiceBusRule(sbn, nsmgr));
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));

WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, ServiceBusIsConnected, namespaceManager.Address.AbsoluteUri));
namespaceUri = namespaceManager.Address;
Expand Down Expand Up @@ -879,15 +894,7 @@ public Uri GetRelayUri(RelayDescription description)
/// <returns>A EventHubDescription handle to the event hub, or null if the event hub does not exist in the service namespace. </returns>
public EventHubDescription GetEventHub(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (namespaceManager != null)
{
return RetryHelper.RetryFunc(() => namespaceManager.GetEventHub(path), writeToLog);
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusEventHub.GetEventHub(path);
}

/// <summary>
Expand All @@ -897,23 +904,7 @@ public EventHubDescription GetEventHub(string path)
/// Returns an empty collection if no event hub exists in this service namespace.</returns>
public Task<IEnumerable<EventHubDescription>> GetEventHubs(int timeoutInSeconds)
{
if (namespaceManager != null)
{
var taskList = new List<Task>();
var task = namespaceManager.GetEventHubsAsync();
taskList.Add(task);
taskList.Add(Task.Delay(TimeSpan.FromSeconds(timeoutInSeconds)));
Task.WaitAny(taskList.ToArray());
if (task.IsCompleted)
{
return task;
}
else
{
throw new TimeoutException();
}
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusEventHub.GetEventHubs(timeoutInSeconds);
}

/// <summary>
Expand All @@ -923,18 +914,7 @@ public Task<IEnumerable<EventHubDescription>> GetEventHubs(int timeoutInSeconds)
/// <returns>Returns a newly-created EventHubDescription object.</returns>
public EventHubDescription CreateEventHub(EventHubDescription description)
{
if (description == null)
{
throw new ArgumentException(DescriptionCannotBeNull);
}
if (namespaceManager != null)
{
var eventHub = RetryHelper.RetryFunc(() => namespaceManager.CreateEventHub(description), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, EventHubCreated, description.Path));
OnCreate?.Invoke(new ServiceBusHelperEventArgs(eventHub, EntityType.EventHub));
return eventHub;
}
throw new ApplicationException(ServiceBusIsDisconnected);
return serviceBusEventHub.CreateEventHub(description);
}

/// <summary>
Expand All @@ -943,20 +923,7 @@ public EventHubDescription CreateEventHub(EventHubDescription description)
/// <param name="path">Path of the event hub relative to the service namespace base address.</param>
public void DeleteEventHub(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException(PathCannotBeNull);
}
if (namespaceManager != null)
{
RetryHelper.RetryAction(() => namespaceManager.DeleteEventHub(path), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, EventHubDeleted, path));
OnDelete?.Invoke(new ServiceBusHelperEventArgs(path, EntityType.EventHub));
}
else
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
serviceBusEventHub.DeleteEventHub(path);
}

/// <summary>
Expand All @@ -965,20 +932,7 @@ public void DeleteEventHub(string path)
/// <param name="eventHubDescription">The event hub to delete.</param>
public void DeleteEventHub(EventHubDescription eventHubDescription)
{
if (string.IsNullOrWhiteSpace(eventHubDescription?.Path))
{
throw new ArgumentException(EventHubDescriptionCannotBeNull);
}
if (namespaceManager != null)
{
RetryHelper.RetryAction(() => namespaceManager.DeleteEventHubAsync(eventHubDescription.Path), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, EventHubDeleted, eventHubDescription.Path));
OnDelete?.Invoke(new ServiceBusHelperEventArgs(eventHubDescription, EntityType.EventHub));
}
else
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
serviceBusEventHub.DeleteEventHub(eventHubDescription);
}

/// <summary>
Expand All @@ -987,14 +941,7 @@ public void DeleteEventHub(EventHubDescription eventHubDescription)
/// </summary>
public void DeleteEventHubs(IEnumerable<string> eventHubs)
{
if (eventHubs == null)
{
return;
}
foreach (var eventHub in eventHubs)
{
DeleteEventHub(eventHub);
}
serviceBusEventHub.DeleteEventHubs(eventHubs);
}

/// <summary>
Expand All @@ -1004,18 +951,7 @@ public void DeleteEventHubs(IEnumerable<string> eventHubs)
/// <returns>Returns an updated EventHubDescription object.</returns>
public EventHubDescription UpdateEventHub(EventHubDescription description)
{
if (description == null)
{
throw new ArgumentException(DescriptionCannotBeNull);
}
if (namespaceManager == null)
{
throw new ApplicationException(ServiceBusIsDisconnected);
}
var eventHub = RetryHelper.RetryFunc(() => namespaceManager.UpdateEventHub(description), writeToLog);
WriteToLogIf(traceEnabled, string.Format(CultureInfo.CurrentCulture, EventHubUpdated, description.Path));
OnCreate?.Invoke(new ServiceBusHelperEventArgs(eventHub, EntityType.EventHub));
return eventHub;
return serviceBusEventHub.UpdateEventHub(description);
}

/// <summary>
Expand All @@ -1025,7 +961,7 @@ public EventHubDescription UpdateEventHub(EventHubDescription description)
/// <returns>The absolute uri of the event hub.</returns>
public Uri GetEventHubUri(string eventHubPath)
{
return Microsoft.ServiceBus.ServiceBusEnvironment.CreateServiceUri(scheme, Namespace, string.Concat(ServicePath, eventHubPath));
return serviceBusEventHub.GetEventHubUri(eventHubPath);
}

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

namespace ServiceBusExplorer.WindowsAzure
{
internal interface IServiceBusEventHub : IServiceBusEntity
{
EventHubDescription CreateEventHub(EventHubDescription description);

void DeleteEventHub(EventHubDescription eventHubDescription);

void DeleteEventHub(string path);

void DeleteEventHubs(IEnumerable<string> eventHubs);

EventHubDescription GetEventHub(string path);

Task<IEnumerable<EventHubDescription>> GetEventHubs(int timeoutInSeconds);

Uri GetEventHubUri(string eventHubPath);

EventHubDescription UpdateEventHub(EventHubDescription description);
}
}
Loading

0 comments on commit f555ed9

Please sign in to comment.