How to purge a MSMQ system queue journal programatically?
Asked Answered
P

3

5

Need to batch a weekly purge of the System queue journal.

Proton answered 30/6, 2009 at 12:27 Comment(0)
T
5

The correct format for system queues:

FormatName:Direct=os:.\\System$;JOURNAL

I've tested this format on Windows 7 and Windows 2003.

(the dot after os: means the localhost/local computer)

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();

or if you want to keep N days of messages you can do this:

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++;
            }
        }
    }
}
Tomi answered 11/5, 2012 at 22:30 Comment(0)
A
2
MessageQueue mq = new MessageQueue(".\\Journal$");
mq.Purge();

or

MessageQueue mq = new MessageQueue(".\\myQueue\\Journal$");
mq.Purge();

if its for a queue you created. Make sure the user has the correct permissions. See: http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.aspx

Agger answered 30/6, 2009 at 12:49 Comment(0)
S
1

Answerd here

MessageQueue mq = new MessageQueue("DIRECT=OS:computername\SYSTEM$;JOURNAL");
mq.Purge();
Showker answered 17/7, 2009 at 21:22 Comment(1)
The correct format: var mq = new MessageQueue("FormatName:Direct=os:.\\System$;JOURNAL");Tomi

© 2022 - 2024 — McMap. All rights reserved.