Skip to content

Commit 205a341

Browse files
committed
Delay queue creation if CheckAndCreateQueues is false for improved startup time
1 parent 24dfa75 commit 205a341

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/HangFire.Azure.ServiceBusQueue/ServiceBusManager.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,26 @@ public ServiceBusManager(ServiceBusQueueOptions options)
2323

2424
_options = options;
2525

26-
_clients = new Dictionary<string, QueueClient>();
26+
_clients = new Dictionary<string, QueueClient>(options.Queues.Length);
2727
_namespaceManager = NamespaceManager.CreateFromConnectionString(options.ConnectionString);
2828
_messagingFactory = MessagingFactory.CreateFromConnectionString(options.ConnectionString);
2929

30-
CreateQueueClients();
30+
// If we have this option set to true then we will create all clients up-front, otherwise
31+
// the creation will be delayed until the first client is retrieved
32+
if (options.CheckAndCreateQueues)
33+
{
34+
CreateQueueClients();
35+
}
3136
}
3237

3338
public QueueClient GetClient(string queue)
3439
{
35-
return this._clients[queue];
40+
if (_clients.Count != _options.Queues.Length)
41+
{
42+
CreateQueueClients();
43+
}
44+
45+
return _clients[queue];
3646
}
3747

3848
public QueueDescription GetDescription(string queue)

src/HangFire.Azure.ServiceBusQueue/ServiceBusQueueOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public ServiceBusQueueOptions()
2727
/// Gets or sets a value which indicates whether or not to automatically create and
2828
/// configure queues.
2929
/// </summary>
30+
/// <remarks>
31+
/// On initialisation if this property is <cc>true</cc> we will create and check all queues
32+
/// immediately, otherwise we delay the creation of the queue clients until they are first
33+
/// requested.
34+
/// </remarks>
3035
public bool CheckAndCreateQueues { get; set; }
3136

3237
public string ConnectionString { get; set; }

0 commit comments

Comments
 (0)