El TecnoBaúl de Kiquenet

Kiquenet boring stories

Introducing MSMQ–MS Queues

Posted by kiquenet en 6 agosto 2014

MSMQ uses flat files located in %windir%\system32\msmq.

If you want to implement your own queueing, I suggest you take a look at Ayende’s blog post on queueing


Surviving poison messages in MSMQ



Invoke-Command -ComputerName "mycomputer" -ScriptBlock {

        ## if public then $queuename = ".\YOUR_Q_NAME" 
        $queuename = ".\private$\YOUR_Q_NAME"  

        [Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
        $queue = New-Object -TypeName "System.Messaging.MessageQueue"
        $queue.Path = $queuename 
        $messagecount = $queue.GetAllMessages().Length
        Write-Host "$queuename has been purged of $messagecount messages."
Write-Host ""
Write-Host "Examples using the .NET System.Messaging assembly to access MSMQ"
Write-Host ""

Write-Host "... load the .NET Messaging assembly"

Write-Host ""

if ([System.Messaging.MessageQueue]::Exists(".\private$\MyQueue"))
  Write-Host "... delete old myqueue"
if ([System.Messaging.MessageQueue]::Exists(".\private$\BtsQueue"))
  Write-Host "... delete old btsqueue"

Write-Host "... create a new queue"
$q1 = [System.Messaging.MessageQueue]::Create(".\private$\MyQueue")

Write-Host "... create new queue, set FullControl permissions for RBC\BIZTALK"
$qb = [System.Messaging.MessageQueue]::Create(".\private$\BtsQueue")


Write-Host "... list existing queues" 
$pqs = [System.Messaging.MessageQueue]::GetPrivateQueuesByMachine(".")
Write-Host "    Count: "$pqs.length  -ForegroundColor gray
foreach($q in $pqs)
    Write-Host "       "$q.QueueName  -ForegroundColor gray

Write-Host "... access existing queue"
$q2 = New-Object System.Messaging.MessageQueue ".\private$\MyQueue"

Write-Host "... adding string Formatter and additional properties "
$q2.Formatter.TargetTypeNames = ,"System.String"
$q2.MessageReadPropertyFilter.ArrivedTime = $true 
$q2.MessageReadPropertyFilter.SentTime = $true 

Write-Host "... create a new High priorty message "
$msg = New-Object System.Messaging.Message "TestMessage"
$msg.label = "Test Msg Label"
$msg.body = "Add some body to test message"
$msg.priority = [System.Messaging.MessagePriority]::High

Write-Host "... send the High message"

$msg.body = "Some more text for the test message"
$msg.priority = [System.Messaging.MessagePriority]::Low

Write-Host "... send the Low message"

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length  -ForegroundColor gray

Write-Host "... peek at queue"
$ts = New-Object TimeSpan 10000000 # 1 sec. timeout just in case MSMQ is empty
$pk = $q2.Peek($ts)
Write-Host "    ArrivedTime: "$pk.ArrivedTime.DateTime -ForegroundColor gray
Write-Host "    SentTime   : "$pk.SentTime.DateTime -ForegroundColor gray

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length -ForegroundColor gray

Write-Host "... receive from queue"
$rmsg = $q2.receive($ts)
Write-Host "    Body : "$rmsg.body  -ForegroundColor gray
Write-Host "    Label: "$rmsg.label -ForegroundColor gray

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length  -ForegroundColor gray

Write-Host "... purge the queue "

Write-Host "... check the queue "
Write-Host "    Count: "$q2.GetAllMessages().length  -ForegroundColor gray

Write-Host ""
Write-Host "All done, but remember to delete the test queues !!"

Queue Journal

var systemJournalQueue = new MessageQueue("FormatName:Direct=os:.\\System$;JOURNAL"); var systemDeadLetterQueue = new MessageQueue("FormatName:Direct=os:.\\System$;DEADLETTER"); var systemDeadXLetterQueue =new MessageQueue("FormatName:Direct=os:.\\System$;DEADXACT")); systemJournalQueue.Purge();
private static void PurgeQueues(int archiveAfterHowManyDays, MessageQueue queue)
    queue.Formatter = new XmlMessageFormatter(new Type[] { typeof(System.String) });
    queue.MessageReadPropertyFilter.ArrivedTime = true;

    using (MessageEnumerator messageReader = queue.GetMessageEnumerator2())
        int counter = 0;
        while (messageReader.MoveNext())
            Message m = messageReader.Current;
            if (m.ArrivedTime.AddDays(archiveAfterHowManyDays) < DateTime.Now)


$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue
$queues | ft -property Name,MessagesInQueue

$host = ...
$cred = get-credential
$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue -computer $host -credential $cred
$queues | ft -property Name,MessagesInQueue
private static int GetMsmqMessageCount(string queuePath, string machine,
  string username, string password)
  var options = new ConnectionOptions
    {Username = username, Password = password};
  var path = string.Format(@"\\{0}\root\CIMv2", machine);
  var scope = new ManagementScope(path, options);

  string queryString = 
    String.Format("SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue WHERE Name = '{0}'",
  var query = new ObjectQuery(queryString);

  var searcher = new ManagementObjectSearcher(scope, query);

  IEnumerable<int> messageCountEnumerable = 
    from ManagementObject queue in searcher.Get()
    select (int) (UInt64) queue.GetPropertyValue("MessagesInQueue");

  return messageCountEnumerable.First();




xcopy, zero administration, queuing service


Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: