Reputation: 2851
I'm using the code below to get all messages from a queue into an array and send to other queues also in an array, what's happening is it sends every message twice to every queue and I can't see why, can anyone see anything obvious?
thanks
public void SendToQs()
{
Code.Class1 c = new Code.Class1();
oInQueue = new MessageQueue(sInQueue);
Message[] msgs = oInQueue.GetAllMessages();
var queueArray = sOutQueues.Select(s => new MessageQueue(s)).ToArray();
foreach (Message msg in msgs)
{
foreach (MessageQueue s in queueArray)
{
c.WriteMessage(s, msg, msg.Label);
}
}
oInQueue.Purge();
}
WriteMessage:
public void WriteMessage(MessageQueue outputQueue, Message msg, string label)
{
if (!outputQueue.Transactional)
{
try
{
outputQueue.Send(msg, label);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
MessageQueueTransaction trans = new MessageQueueTransaction();
try
{
trans.Begin();
outputQueue.Send(msg, label, trans);
trans.Commit();
}
catch (Exception ex)
{
Console.WriteLine("message Q exception" + ex.Message);
trans.Abort();
}
}
}
Upvotes: 1
Views: 873
Reputation: 2851
Got it, and it was a daft as I was expecting! In my void Main() I had originally kicked off a process just to make sure it worked. I then added a line to start a new thread with this process, forgetting to take the original one out, so it was running twice. DOH!
Upvotes: 1
Reputation: 19407
I have not had time to test this but you may want to consider this.
If a queue is having all it's messages sent to the other queues, then, when iterating through the list of queues - the original queue is also sent this message.
foreach (Message msg in msgs)
{
foreach (MessageQueue s in queueArray)
{
if (s.Id == oInQueue.Id) continue; // Skip if this is the originator
c.WriteMessage(s, msg, msg.Label);
}
}
Upvotes: 0