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

http://www.cogin.com/articles/SurvivingPoisonMessages.php

Purgue:

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
        $queue.Purge()
        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"
[Reflection.Assembly]::LoadWithPartialName("System.Messaging")

Write-Host ""

if ([System.Messaging.MessageQueue]::Exists(".\private$\MyQueue"))
  {
  [System.Messaging.MessageQueue]::Delete(".\private$\MyQueue")
  Write-Host "... delete old myqueue"
  }
if ([System.Messaging.MessageQueue]::Exists(".\private$\BtsQueue"))
  {
  [System.Messaging.MessageQueue]::Delete(".\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")

$qb.SetPermissions("RBC\BIZTALK", 
      [System.Messaging.MessageQueueAccessRights]::FullControl,            
      [System.Messaging.AccessControlEntryType]::Set)

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"
$q2.send($msg)

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

Write-Host "... send the Low message"
$q2.send($msg)

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 "
$q2.Purge()

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)
            {
                queue.ReceiveById(m.Id);
                counter++;
            }
        }
    }
}

http://www.meadow.se/wordpress/retrieving-the-message-count-is-msmq-queues/comment-page-1/#comment-473136
http://jopinblog.wordpress.com/2008/03/12/counting-messages-in-an-msmq-messagequeue-from-c/

$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);
  scope.Connect();

  string queryString = 
    String.Format("SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue WHERE Name = '{0}'",
	  queuePath);
  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();
}
 

References:
http://stackoverflow.com/questions/802661/how-does-msmq-manage-messages?rq=1

http://stackoverflow.com/questions/25011855/how-purge-messages-from-ms-queue-in-remote-server-using-script
http://stackoverflow.com/questions/765954/setting-permissions-on-a-msmq-queue-in-script/915127#915127
http://stackoverflow.com/questions/11533690/how-message-get-purged-from-poison-queue-in-msmq
http://stackoverflow.com/questions/1063378/msmq-how-to-purge-system-queue-journal-programaticaly
http://stackoverflow.com/questions/11793441/purge-msmq-queue-and-reset-iis-from-a-bat-file

http://stackoverflow.com/questions/20344562/create-private-message-queue-on-remote-machine
http://blogs.msdn.com/b/johnbreakwell/

xcopy, zero administration, queuing service
http://ayende.com/blog/3480/rhino-queues

Anuncios

Responder

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 )

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 )

Google+ photo

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

Conectando a %s

 
A %d blogueros les gusta esto: