Last active
February 22, 2021 04:29
-
-
Save jcefoli/069f180b251d94c7ebc49b979c5c035c to your computer and use it in GitHub Desktop.
Get MSMQ Queue Size and Copy All Messages to Another Queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null | |
#Define Queue Connection Info Here (In this case, we're passing the IP to a clustered MSMQ instance) | |
$fromQueueConnString = "FormatName:Direct=TCP:172.1.1.1\private$\queue_to_copy_messages_from" | |
$toQueueConnString = "FormatName:Direct=TCP:172.1.1.1\private$\queue_to_copy_messages_to" # Make sure this queue exists - I don't create it and it might fail | |
# Get Queue Size - https://stackoverflow.com/a/2291087 | |
$qsource = @" | |
public class QueueSizer | |
{ | |
public static System.Messaging.Message PeekWithoutTimeout(System.Messaging.MessageQueue q, System.Messaging.Cursor cursor, System.Messaging.PeekAction action) | |
{ | |
System.Messaging.Message ret = null; | |
try | |
{ | |
// Peek at the queue, but timeout in one clock tick. | |
ret = q.Peek(new System.TimeSpan(1), cursor, action); | |
} | |
catch (System.Messaging.MessageQueueException mqe) | |
{ | |
// Trap MSMQ exceptions but only ones relating to timeout. Bubble up any other MSMQ exceptions. | |
if (!mqe.Message.ToLower().Contains("timeout")) | |
{ | |
throw; | |
} | |
} | |
return ret; | |
} | |
// Main message counting method. | |
public static int GetMessageCount(string queuepath) | |
{ | |
// Get a specific MSMQ queue by name. | |
System.Messaging.MessageQueue q = new System.Messaging.MessageQueue(queuepath); | |
int count = 0; | |
// Create a cursor to store the current position in the queue. | |
System.Messaging.Cursor cursor = q.CreateCursor(); | |
// Have quick peak at the queue. | |
System.Messaging.Message m = PeekWithoutTimeout(q, cursor, System.Messaging.PeekAction.Current); | |
if (m != null) | |
{ | |
count = 1; | |
// Keep on iterating through the queue and keep count of the number of messages that are found. | |
while ((m = PeekWithoutTimeout(q, cursor, System.Messaging.PeekAction.Next)) != null) | |
{ | |
count++; | |
} | |
} | |
// Return the tally. | |
return count; | |
} | |
} | |
"@ | |
# Add the new QueueSizer class helper to the environment. | |
Add-Type -TypeDefinition $qsource -ReferencedAssemblies "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Messaging.dll" | |
# Call the helper and get the message count. | |
$fromQueueSize = [QueueSizer]::GetMessageCount($fromQueueConnString); | |
$FromQueueObject = new-object System.Messaging.MessageQueue $fromQueueConnString | |
$DestinationQueueObject = new-object System.Messaging.MessageQueue $toQueueConnString | |
$messageCount = 0 | |
do | |
{ | |
$DestinationQueueObject.Send($FromQueueObject.Receive()) | |
$messageCount = $messageCount + 1 | |
} | |
while ($messageCount -lt $fromQueueSize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment