Thursday, May 14, 2009

PowerShell to set up MSMQ private queues for MassTransit

A pretty self explanatory title: I want to be able to create MSMQ private queues on the fly. I am currently playing with Mass Transit 0.6 and there are a couple of queues i needed to create and would like them to be done upfront. As far as i am aware there you have to go into the config of each project and get the names of each queue and manually create them. This script means i don't have to (I switch machines a lot):

CAVEAT: I am not a PS guru, in fact i am a complete n00b, combine that with my 101 knowledge of MSMQ and this is probably a disaster; you have been warned.

#Beginning of MassTransitSetup.ps1#
param(
[string] $nameofQ, [string]$username
)
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Messaging")

function CreatePrivateMSMQQueue {
param ( [string]$queuename = $(throw "Please specify the queue name!"),
[string]$user)

if ([System.Messaging.MessageQueue]::Exists($queuename))
{
write-host "$queuename already exists"
}
else
{
$newQueue = [System.Messaging.MessageQueue]::Create($queuename)
if ([System.Messaging.MessageQueue]::Exists($queuename))
{
write-host "$queuename has been created"
$newQueue.Label = $queuename
#Default to everyone if no user is specified
if([string]::IsNullOrEmpty($user)){$user = "Everyone"}
write-host "Setting permissions for user : $user"
$newQueue.SetPermissions(
$user,
[System.Messaging.MessageQueueAccessRights] "ReceiveMessage, PeekMessage, GetQueueProperties, GetQueuePermissions")
}
else
{
write-host "$queuename could not be created!!!"
}
}
}

function CreateDefaultMassTransitQueues{
param ( [string]$user)

$deaultqueues = ".\private$\mt_client", ".\private$\mt_server", ".\private$\mt_server1", ".\private$\mt_subscriptions"
foreach($i in $deaultqueues)
{
CreatePrivateMSMQQueue $i $user
}
}

#Begining of script
if([string]::IsNullOrEmpty($nameofQ))
{
CreateDefaultMassTransitQueues $username
}
else
{
CreatePrivateMSMQQueue $nameofQ $username
}


For more info on MassTransit see the google code home page and be sure to check out the wiki on how to set up the starbucks sample. If you are running the starbucks sample set the queues in the scripts above to:

$deaultqueues = ".\private$\mt_client",
".\private$\mt_server",
".\private$\mt_server1",
".\private$\mt_subscriptions",
".\private$\mt_subscription_ui",
".\private$\mt_health",
".\private$\mt_timeout"

No comments: