Skip to content

Instantly share code, notes, and snippets.

@ajai8085
Created December 9, 2015 03:45
Show Gist options
  • Save ajai8085/beec125fda2a59df26b8 to your computer and use it in GitHub Desktop.
Save ajai8085/beec125fda2a59df26b8 to your computer and use it in GitHub Desktop.
Msmq Test (Normal, Transactional, Pub-Subscriber Sample
using System;
using System.Messaging;
namespace TestTransactionalQueue
{
class Program
{
private static MessageQueue GetQueue(bool trans=true ,string path = @".\Private$\TestTran")
{
MessageQueue mq;
if (MessageQueue.Exists(path))
{
mq = new System.Messaging.MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path, trans );
mq.Label = "you are the label";
}
return mq;
}
static void Main(string[] args)
{
using (var queue= GetQueue())
{
using (var trans = new MessageQueueTransaction())
{
trans.Begin();
var m = new Message { Body = "hello" };
m.Label = typeof(string).FullName ;
m.Recoverable = true;
queue.Send(m, trans);
trans.Commit();
}
}
Console.WriteLine("Written trans");
using (var queue = GetQueue(false, @".\Private$\Notrans"))
{
var m = new Message { Body = "hello" };
m.Label = typeof(string).FullName ;
m.Recoverable = true;
queue.Send(m );
}
Console.WriteLine("Written non trans");
using (var queue = GetQueue(false, @".\Private$\Subscriber1"))
{
queue.MulticastAddress = "236.1.1.6:8002";
//var m = new Message { Body = "hello" };
//m.Recoverable = true;
//queue.Send(m);
}
using (var queue = GetQueue(false, @".\Private$\Subscriber2"))
{
queue.MulticastAddress = "236.1.1.6:8002";
//var m = new Message { Body = "hello" };
//m.Recoverable = true;
//queue.Send(m);
}
using (MessageQueue udp = new MessageQueue("FormatName:MULTICAST=236.1.1.6:8002"))
{
var m = new Message { Body = "hellokkk" };
m.Label = typeof(string).FullName ;
m.Recoverable = true;
udp.Send(m);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment