Skip to content

Instantly share code, notes, and snippets.

@MartinBodocky
Last active August 29, 2015 14:17
Show Gist options
  • Save MartinBodocky/97e10a3b739bda3cec2d to your computer and use it in GitHub Desktop.
Save MartinBodocky/97e10a3b739bda3cec2d to your computer and use it in GitHub Desktop.
MSMQ Administration with F#
#I @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\"
#r "System.Messaging.dll"
open System
open System.Messaging
open System.Diagnostics
// create message queue
MessageQueue.Create(@".\private$\r_default-queue")
// create transactional queue
MessageQueue.Create(@".\private$\r_transactional-queue", true)
// create security queue - WARNING you cannot use secure queue unless you don't have certificate provider set up with MSMQ
MessageQueue.Create(@".\private$\r_secure-queue", true)
let secureQueue = new MessageQueue(@".\private$\r_secure-queue")
secureQueue.Authenticate <- true
secureQueue.EncryptionRequired <- EncryptionRequired.Body
// test durable queue
MessageQueue.Create(@".\private$\r_durable-queue")
//testing - these message are stored just into memory
// after restarting Messaging Queue process, all of them are gone
let queue = new MessageQueue(@".\private$\r_default-queue")
let stopWatch = new Stopwatch()
stopWatch.Start()
[ 1..1000 ] |> List.iter (fun i -> queue.Send("Message: " + i.ToString()))
stopWatch.Stop()
printfn "Milliseconds %A" stopWatch.ElapsedMilliseconds
//durable queue
let d_queue = new MessageQueue(@".\private$\r_durable-queue")
d_queue.DefaultPropertiesToSend.Recoverable <- true
let d_stopWatch = new Stopwatch()
d_stopWatch.Start()
[ 1..1000 ] |> List.iter (fun i -> d_queue.Send("Message: " + i.ToString()))
d_stopWatch.Stop()
printfn "Milliseconds %A" d_stopWatch.ElapsedMilliseconds
//transaction queue
let t_queue = new MessageQueue(@".\private$\r_transactional-queue")
let t_stopWatch = new Stopwatch()
t_stopWatch.Start()
[ 1..1000 ] |> List.iter (fun i ->
let tx = new MessageQueueTransaction()
tx.Begin()
t_queue.Send("Message: " + i.ToString(), tx)
tx.Commit())
t_stopWatch.Stop()
printfn "Milliseconds %A" t_stopWatch.ElapsedMilliseconds
//transaction queue with one big transaction
let tt_queue = new MessageQueue(@".\private$\r_transactional-queue")
let tt_stopWatch = new Stopwatch()
tt_stopWatch.Start()
let tx = new MessageQueueTransaction()
tx.Begin()
[ 1..1000 ] |> List.iter (fun i -> t_queue.Send("Message: " + i.ToString(), tx))
tx.Commit()
tt_stopWatch.Stop()
printfn "Milliseconds %A" tt_stopWatch.ElapsedMilliseconds
// remove all messages in queues
let queues = [| @".\private$\r_default-queue"; @".\private$\r_transactional-queue"; @".\private$\r_secure-queue"; @".\private$\r_durable-queue" |]
queues
|> Array.iter(fun q -> (new MessageQueue(q)).Purge())
// sending multicast messages
let multiQueue = new MessageQueue("FormatName:MULTICAST=234.1.1.1:8001")
multiQueue.DefaultPropertiesToSend.Recoverable <- true
multiQueue.Send("Hallo "+ DateTime.Now.ToString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment