MSMQ explore(one)
•Introduction
•It's like emailing!
•A quick start sample
•Message Queue Management
•Sending Messages
•The Message Object
•Dead Letter Queue and Acknowledge Types
•Receiving Messages
•Using Transactions
Introduction
IPC(inter-process communication)
Introduction
IPC(inter-process communication)
A set of techniques for the exchange of data between two or more threads in one or more processes. Processes may be running on one computer or on two or more computers connected by a network
Approach
Approach
Exchange files via a network share。
Custom socket
.NET Remoting and Web Services
Message Queue
Remoting/Web Service VS. Message Queue
Robustness
Robustness
Message prioritization
Offline capabilities
Transactional messaging
Security
Microsoft's Message Queuing (MSMQ) is such a message server and is part of the Windows Server operating system。
Microsoft's Message Queuing (MSMQ) is such a message server and is part of the Windows Server operating system。
Originally MSMQ came with a native, COM-based programming interface. But Microsoft has added a.NET wrapper on top of it which makes the development of MSMQ-based systems quite easier The first version of MSMQ was delivered with Windows NT 4.0; now Windows XP/2003 brings the newest version (3.0), provding several interesting new features.
it's like email:
![](/images/cnblogs_com/jigee/qq.png)
A quick start sample
Queue Type
it's like email:
![](/images/cnblogs_com/jigee/qq.png)
A quick start sample
using System;
using System.Messaging;
namespace WindowsApplication3
{
class MsmQuickstart
{
[STAThread]
public static void Main(string[] args)
{
// SendMessage();
// Foo();
Bar();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
private static void SendMessage()
{
// (1) path to message queue
// Syntax: [computer name]\private$\[queue name]
// use ".' for localhost
string queuePath = @".\private$\TSS_MsmqTest";
// (2) open message queue, create new one if not exists
MessageQueue messageQueue;
if (MessageQueue.Exists(queuePath))
{
messageQueue = new MessageQueue(queuePath);
}
else
{
messageQueue = MessageQueue.Create(queuePath);
}
Console.WriteLine("Message Queue is ready.");
// (3) send simple message
messageQueue.Send("Hello World!");
// (4) break here and have a look in you messaeg queue!!
// System.Diagnostics.Debugger.Break();
Console.ReadLine();
// (5) reveive message from message queue
Message message = messageQueue.Receive();
string myMessage = Convert.ToString(message.Body);
Console.WriteLine(myMessage);
// (6) Close message queue
messageQueue.Close();
}
private static void Foo()
{
// (1) path to message queue
string queuePath = @".\private$\TSS_MsmqTest_QueueAdministration";
// (2) create new queue
MessageQueue.Create(queuePath);
// (3) check if queue exists
bool queueExists = MessageQueue.Exists(queuePath);
// (4) Remove all message from queue
MessageQueue messageQueueA = new MessageQueue(queuePath);
Console.WriteLine("Message Queue is ready.");
messageQueueA.Send("Hello World!");
messageQueueA.Purge();
messageQueueA.Close();
// (5) delete existing message queue
Console.ReadLine();
MessageQueue.Delete(queuePath);
// (6) set permissions to message queue
/*
MessageQueue messageQueueB = new MessageQueue(queuePath);
messageQueueB.SetPermissions("Rocket Liu", MessageQueueAccessRights.ReceiveMessage);
messageQueueB.Close();
*/
}
private static void Bar()
{
// (1) define message pathes
string queuePath = @".\private$\TSS_MsmqTest";
string queuePathAdmin = @".\private$\TSS_MsmqTest_QueueAdministration";
// (2) Create message object and set properties
Message message = new Message();
message.UseDeadLetterQueue = true;
message.TimeToBeReceived = new TimeSpan(0, 0, 10); // 10s
message.AdministrationQueue = MessageQueue.Create(queuePathAdmin);
message.AcknowledgeType = AcknowledgeTypes.NegativeReceive;
message.Body = "Hello World!";
Console.WriteLine("Message Queue is ready.");
// (3) send message
MessageQueue messageQueue = new MessageQueue(queuePath);
messageQueue.Send(message);
messageQueue.Close();
}
}
}
Message Queue Management using System.Messaging;
namespace WindowsApplication3
{
class MsmQuickstart
{
[STAThread]
public static void Main(string[] args)
{
// SendMessage();
// Foo();
Bar();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
}
private static void SendMessage()
{
// (1) path to message queue
// Syntax: [computer name]\private$\[queue name]
// use ".' for localhost
string queuePath = @".\private$\TSS_MsmqTest";
// (2) open message queue, create new one if not exists
MessageQueue messageQueue;
if (MessageQueue.Exists(queuePath))
{
messageQueue = new MessageQueue(queuePath);
}
else
{
messageQueue = MessageQueue.Create(queuePath);
}
Console.WriteLine("Message Queue is ready.");
// (3) send simple message
messageQueue.Send("Hello World!");
// (4) break here and have a look in you messaeg queue!!
// System.Diagnostics.Debugger.Break();
Console.ReadLine();
// (5) reveive message from message queue
Message message = messageQueue.Receive();
string myMessage = Convert.ToString(message.Body);
Console.WriteLine(myMessage);
// (6) Close message queue
messageQueue.Close();
}
private static void Foo()
{
// (1) path to message queue
string queuePath = @".\private$\TSS_MsmqTest_QueueAdministration";
// (2) create new queue
MessageQueue.Create(queuePath);
// (3) check if queue exists
bool queueExists = MessageQueue.Exists(queuePath);
// (4) Remove all message from queue
MessageQueue messageQueueA = new MessageQueue(queuePath);
Console.WriteLine("Message Queue is ready.");
messageQueueA.Send("Hello World!");
messageQueueA.Purge();
messageQueueA.Close();
// (5) delete existing message queue
Console.ReadLine();
MessageQueue.Delete(queuePath);
// (6) set permissions to message queue
/*
MessageQueue messageQueueB = new MessageQueue(queuePath);
messageQueueB.SetPermissions("Rocket Liu", MessageQueueAccessRights.ReceiveMessage);
messageQueueB.Close();
*/
}
private static void Bar()
{
// (1) define message pathes
string queuePath = @".\private$\TSS_MsmqTest";
string queuePathAdmin = @".\private$\TSS_MsmqTest_QueueAdministration";
// (2) Create message object and set properties
Message message = new Message();
message.UseDeadLetterQueue = true;
message.TimeToBeReceived = new TimeSpan(0, 0, 10); // 10s
message.AdministrationQueue = MessageQueue.Create(queuePathAdmin);
message.AcknowledgeType = AcknowledgeTypes.NegativeReceive;
message.Body = "Hello World!";
Console.WriteLine("Message Queue is ready.");
// (3) send message
MessageQueue messageQueue = new MessageQueue(queuePath);
messageQueue.Send(message);
messageQueue.Close();
}
}
}
Public Queue
Private Queue
Journal Queue
Dead-Letter Queue
Message Queue Management
System.Messaging.MessageQueue
Methods:
Create
Delete
Exists
Purge
SetPermissions
Sending Messages
MessageQueue.Send()
the source code down load address:
/Files/jigee/msmqDemoCode.rar
...............not be over!................!
Sending Messages
MessageQueue.Send()
the source code down load address:
/Files/jigee/msmqDemoCode.rar
...............not be over!................!