Skip to content

Commit b71c4ac

Browse files
author
John Simons
committed
Merge branch 'hotfix-3.3.10' into support-3.3
2 parents 29cc04d + 0e5ded0 commit b71c4ac

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/distributor/NServiceBus.Distributor.MsmqWorkerAvailabilityManager/MsmqWorkerAvailabilityManager.cs

+29-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using NServiceBus.Unicast.Distributor;
33
using System.Messaging;
44
using NServiceBus.Utils;
@@ -12,7 +12,7 @@ namespace NServiceBus.Distributor.MsmqWorkerAvailabilityManager
1212
public class MsmqWorkerAvailabilityManager : IWorkerAvailabilityManager
1313
{
1414
MessageQueue storageQueue;
15-
15+
object lockobject = new object();
1616
/// <summary>
1717
/// Sets the path to the queue that will be used for storing
1818
/// worker availability.
@@ -29,11 +29,13 @@ public class MsmqWorkerAvailabilityManager : IWorkerAvailabilityManager
2929
/// </param>
3030
public void ClearAvailabilityForWorker(Address address)
3131
{
32-
var existing = storageQueue.GetAllMessages();
33-
34-
foreach (var m in existing)
35-
if (MsmqUtilities.GetIndependentAddressForQueue(m.ResponseQueue) == address)
36-
storageQueue.ReceiveById(m.Id, MessageQueueTransactionType.Automatic);
32+
lock(lockobject)
33+
{
34+
var existing = storageQueue.GetAllMessages();
35+
foreach(var m in existing)
36+
if(MsmqUtilities.GetIndependentAddressForQueue(m.ResponseQueue) == address)
37+
storageQueue.ReceiveById(m.Id, MessageQueueTransactionType.Automatic);
38+
}
3739
}
3840

3941
/// <summary>
@@ -44,14 +46,17 @@ public Address PopAvailableWorker()
4446
{
4547
try
4648
{
47-
var m = storageQueue.Receive(TimeSpan.Zero, MessageQueueTransactionType.Automatic);
48-
49-
if (m == null)
49+
Message m;
50+
lock (lockobject)
51+
m = storageQueue.Receive(TimeSpan.Zero, MessageQueueTransactionType.Automatic);
52+
53+
if(m == null)
5054
return null;
51-
55+
5256
return MsmqUtilities.GetIndependentAddressForQueue(m.ResponseQueue);
57+
5358
}
54-
catch (Exception)
59+
catch(Exception)
5560
{
5661
return null;
5762
}
@@ -65,9 +70,9 @@ public void Start()
6570
var path = MsmqUtilities.GetFullPath(StorageQueueAddress);
6671

6772
storageQueue = new MessageQueue(path);
68-
69-
if (!storageQueue.Transactional)
70-
throw new Exception("Queue must be transactional.");
73+
74+
if(!storageQueue.Transactional)
75+
throw new Exception("Queue must be transactional.");
7176
}
7277

7378
/// <summary>
@@ -79,11 +84,14 @@ public void Start()
7984
/// <param name="capacity">The number of messages that this worker is ready to process</param>
8085
public void WorkerAvailable(Address address, int capacity)
8186
{
82-
for (var i = 0; i < capacity; i++)
83-
storageQueue.Send(new Message
84-
{
85-
ResponseQueue = new MessageQueue(MsmqUtilities.GetFullPath(address))
86-
}, MessageQueueTransactionType.Automatic);
87+
lock(lockobject)
88+
{
89+
for(var i = 0; i < capacity; i++)
90+
storageQueue.Send(new Message
91+
{
92+
ResponseQueue = new MessageQueue(MsmqUtilities.GetFullPath(address))
93+
}, MessageQueueTransactionType.Automatic);
94+
}
8795
}
8896
}
89-
}
97+
}

0 commit comments

Comments
 (0)