Directory
- One: Some of the theoretical knowledge of MSMQ
- Two: queue type (queuing type)
- Three: Install Message Queuing
- Four: MessageQueue class in C #
- V: msmq-send message to remote private queue
- VI: Example
first, before learning the MessageQueue class, introduce some of the theoretical knowledge of MSMQ
MSMQ (Microsoft Message Queue, Microsoft messaging queuing) Official explanation: An Asynchronous transfer mode that communicates with multiple different applications, which can be distributed on the same machine, It can also be distributed in any location in the connected network space. MSMQ makes communication between applications faster and more reliable by sending and receiving messages.
Its implementation principle is that the sender of the message puts the information he wants to send into a container (which we call a message) and then saves it to a system public space in the message queue A local or offsite message receiver is then taken out of the queue to process messages sent to it.
in the messaging mechanism, there are two more important concepts. One is the message, one is the queue. Message is by the communication between the two sides need to pass the information, plainly it can be a variety of media, such as text, sound, image, in our programming, it is generally a class of objects or strings, etc., the message ultimately understand the way, for the message passing the two sides agreed beforehand, the advantage is that, One is the equivalent of simple encryption of the data, and two in their own definition of the format can save the transmission of communication. A queue is a common storage space for sending and receiving messages, which can exist in memory or in a physical file.
The benefit of using MSMQ is: because it is asynchronous communication, Neither the sender nor the receiver can execute the rest of the code without waiting for the other party to return the success message, thus greatly improving the ability of the processing of the things; the information sending mechanism has some function in the fault recovery ability when the information is transmitted. MSMQ's messaging mechanism makes it possible for both sides of the message communication to have different physical platforms. Using the MSMQ functionality provided on Microsoft's. NET platform, you can easily create or delete message queues, send or receive messages, and even manage message queues.
Two: queue type (queuing type)
Queues and system queues that are created by you or by other users on your network. A user-created queue may be any one of the following queues:
The public queue is replicated throughout the Message Queuing network and may be accessed by all sites that are connected by the network.
Private queues are not published throughout the network. Instead, they are available only on the local computer where they reside. A private queue can only be accessed by an application that knows the full path name or label of the queue.
The management queue contains messages confirming the receipt of messages sent in a given Message Queuing network. Specifies the management queue (if any) that you want the MessageQueue component to use.
The response queue contains the response message that is returned to the sending application when the message is received by the target application. Specifies the response queue, if any, that you want the MessageQueue component to use.
System-generated queues generally fall into the following categories: The journal queue optionally stores a copy of the sent message and a copy of the message that is removed from the queue. A single journal queue on each Message Queuing client stores a copy of the message sent from that computer. A separate journal queue is created for each queue on the server. This journal tracks messages that have been removed from the queue. The dead-letter queue stores a copy of a message that cannot be delivered or has expired. If the expired or undeliverable message is a transactional message, it is stored in a special dead-letter queue, called a transactional dead-letter queue. Badmail is stored on the same computer as the expired message. For more information about time-out periods and expired messages, see Default message properties. The report queue contains a message that indicates the route the message has reached to the destination, and can also contain a test message. There can be only one report queue on each computer. A "dedicated system queue" is a series of private queues that the storage system needs to manage and notify messages that are required to perform message processing operations. Most of the work done in the application involves accessing the public queue and its messages. However, depending on the application's journaling, acknowledgments, and other special processing needs, several different system queues are likely to be used in daily operations.
Three: install Message Queuing
If you're using Message Queuing to communicate, you'll have to install Message Queuing on your PC, install it by component, and the exact installation method is an unknown one, similar to installing IIS
With a simple understanding of Message Queuing, it is necessary to install MSMQ for software development using MSMQ, which should go into the actual development phase after installation. The specific installation process is in the Control Panel under "Add/Remove Programs" under "Add/Remove Windows Components", complete the add is OK. After the installation is complete, a new message queue can be added through the interaction interface, as detailed below:
Out of the interface above to create MSMQ, can also be programmed to complete.
Four: MessageQueue class in C #
When you use. NET to develop MSMQ, you must introduce a named null System.Messaging
The MessageQueue supports two types of messages, synchronous and asynchronous, using Peek (), receive (), and asynchronous using:
Beginpeek () and Beginreceive (), there is no essential difference between the two, are encapsulated good method, you just need to directly create MessageQueue object to call this method is OK, I think as to what synchronous and asynchronous what meaning, there is no need to explain it,
V: msmq-send message to remote private queue
In workgroup mode, the private queue is accessed remotely. Found an article on the Internet, translated a bit. Finally, using the multi-element format name method, the IP address is used to send messages directly to single or multiple targets MessageQueue RmQ = new MessageQueue ("formatname:direct=tcp:121.0.0.1// Private$//queue,direct=tcp:192.168.1.2//private$//queue "); Rmq.send (" Sent to regular Queue-atul "), original address http:/ Www.infosysblogs.com/microsoft/2007/02/msmq_sending_message_to_remote.html 1. When you need to reference a remote queue, use the machinename/ Private$/queuename "format is not working. The "Invalid queue path" error is returned. 2. The queue name needs to be in the format "Formatname:direct=os:machinename//private$//queuename". Other friendly forms of expressions are called after they are converted to the FormatName format. and other these friendly expressions are parsed based on active Directory (domain), and if there is no domain support, these expressions will not work. Example: messagequeue RmQ = new messagequeue &NBSP ; ("formatname:direct=os:machinename//private$ Queue "); rmq.send (" sent to regular Queue-atul "); 3.fontname is case-sensitive. If the expression is "formatname:direct=os:machinename//private$//queuename", it is a non-Legal work. However, this expression does not return any errors. FontName seems to be the only case-sensitive part of an expression. Other parts are free to use case, for example, you can use "DIRECT". 4. If you want to use the machine IP address, the syntax for the expression is "formatname:direct=tcp:ipaddress//private$//queuename" Example: MessageQueue RmQ = new messagequeue &NBSP ; ("Formatname:direct=tcp:121.0.0.1//private$//queue"), & nbsp; Rmq.send ("sent to regular Queue-atul") 5. The transactional properties of a queue instance object created in code must match the properties of the destination queue to be sent. The message sent in the previous example is a non-transactional message, if you want to send a message to a transactional queue, the code is: messagequeue rmtxnq = new messagequeue , &NB Sp ("Formatname:direct=os:machinename//private$//queue"); rmtxnq.send ("Sent to Txn Queue-atul ", Messagequeuetransactiontype.single); If the transaction property does not match, the message cannot be delivered. The system does not return any errors, but the message is discarded. 6. MostWhen you send a message to a remote queue, the system creates a temporary outgoing queue on the local computer. This is done to prevent remote queues from being inaccessible. You can see these temporary queues in Computer Manager by viewing Message Queuing/outgoing queues. Status (Unicom, non-connected) and IP addresses can be displayed on the right side of the manager.
VI: Example
------------------------------------------------------------------------------
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Messaging;
Namespace Myqueue
{
public class myMessageQueue
{
private string Path;
<summary>
1. Create a new message queue with the specified path by creating method
</summary>
<param name= "QueuePath" ></param>
public void Createqueue (string queuepath)
{
Try
{
if (! Messagequeue.exists (QueuePath))
{
Messagequeue.create (QueuePath);
}
Else
{
Console.WriteLine (QueuePath + "already exists! ");
Messagequeue.delete (QueuePath);
Messagequeue.create (QueuePath);
Console.WriteLine (queuepath + "delete rebuild");
}
Path = QueuePath;
}
catch (Messagequeueexception e)
{
Console.WriteLine (E.message);
}
}
<summary>
2. Connect Message Queuing and send messages to the queue
Remote mode: MessageQueue RmQ = new MessageQueue ("Formatname:direct=os:machinename//private$//queue");
Rmq.send ("Sent to regular Queue-atul"); MSMQ for extranet can only be sent
</summary>
public void SendMessage ()
{
Try
{
Connect to a local queue
MessageQueue myqueue = new MessageQueue (Path);
MessageQueue myqueue = new MessageQueue ("formatname:direct=tcp:192.168.12.79//private$//myqueue1");
MessageQueue RmQ = new MessageQueue ("Formatname:direct=tcp:121.0.0.1//private$//queue");--Remote format
Message Mymessage = new Message ();
Mymessage.body = "message content 34kuangbo to die";
Mymessage.formatter = new XMLMessageFormatter (new type[] {typeof (String)});
A message occurs in the queue
Myqueue.send (Mymessage);
Console.WriteLine ("Message sent successfully!") ");
Console.ReadLine ();
}
catch (ArgumentException E)
{
Console.WriteLine (E.message);
}
}
<summary>
3. Connect the message queue and receive messages from the queue
</summary>
public void ReceiveMessage ()
{
MessageQueue myqueue = new MessageQueue (Path);
Myqueue.formatter = new XMLMessageFormatter (new type[] {typeof (String)});
Try
{
Receiving messages from the queue
Message Mymessage = Myqueue.receive ();//Myqueue.peek ();--no message removed from queue after receiving
String context = MyMessage.Body.ToString ();
Console.WriteLine ("Message content:" + context);
Console.ReadLine ();
}
catch (Messagequeueexception e)
{
Console.WriteLine (E.message);
}
catch (InvalidCastException e)
{
Console.WriteLine (E.message);
}
}
<summary>
4. Emptying messages for the specified queue
</summary>
public void Clealmessage ()
{
MessageQueue myqueue = new MessageQueue (Path);
Myqueue.purge ();
Console.WriteLine ("All messages on {0} have been emptied", Path);
}
<summary>
5. Connect the queue and get all the messages for the queue
</summary>
public void Getallmessage ()
{
MessageQueue myqueue = new MessageQueue (Path);
message[] Allmessage = Myqueue.getallmessages ();
XMLMessageFormatter formatter = new XMLMessageFormatter (new type[] {typeof (String)});
for (int i = 0; i < allmessage.length; i++)
{
Allmessage[i]. Formatter = Formatter;
Console.WriteLine ("{0} Confidential message is: {1}", I+1, Allmessage[i]. Body.tostring ());
}
Console.ReadLine ();
}
}
}
------------------------using system;using system.collections.generic;using system.text;namespace myqueue{class program {static void Main (string[] args) {mymessagequeue queue = new myMessageQueue (); Queue. Createqueue (".//private$//myqueue2"); Queue. SendMessage (); Queue. Getallmessage (); Queue. ReceiveMessage (); Queue. Clealmessage (); }}} Note: The path of the transfer is formatted correctly.
Source: http://blog.csdn.net/sage425/article/details/6298461
----examples of using MSMQ in. Net