The previous article said that building a VMware virtual machine to communicate with the host, the environment is ready, now can do the distributed development of MSMQ.
This article is about four points for MSMQ:
1.MSMQ Introduction
Installation of 2.MSMQ
3.MSMQ Programming Development
4.Demo Downloads
I. INTRODUCTION of MSMQ
MSMQ is essentially a messaging protocol that allows reliable communication between applications running on separate service-side/clients. Queues are used to temporarily store messages, the server sends messages to the queue, and the client reads messages from the queue. This makes it possible to communicate through the queue even if the server and client are not on the same network and cannot be accessed directly from each other. By contrast, sockets and other network protocols cannot do this, they only work if the server side and the client can communicate directly with each other.
MSMQ was born in 1997 and has been widely used in enterprise-class software. Microsoft has incorporated MSMQ into its message technology framework, WCF. In WCF, MSMQ is used to provide secure, reliable transport and a unified programming model, while also compatible with communication standards.
MSMQ is responsible for the reliable delivery of information between internal and external applications in the enterprise. MSMQ places the failed message in a queue, and MSMQ sends the message back to the target once the target is reachable. It also supports the security and prioritization mechanisms of messages. If you want the message to time out or for other reasons to fail, you can create a dead-letter queue for viewing.
MSMQ also supports transactions. It allows multiple operations on multiple queues to be carried out in a single transaction, guaranteeing either no execution or no execution at all. The Microsoft Distributed Transaction Coordinator (MSDTC) supports transactional access to MSMQ and other resources.
Microsoft Message Queuing uses the following ports:
- tcp:1801
- rpc:135, 2101*, 2103*, 2105*
- UDP: 3527, 1801
- When Message Queuing is initialized, these port numbers may be increased by 11 if RPC ports are in use. Query port 135 discovers the 2XXX port.
The MSMQ versions used in each operating system are as follows:
- Version 1.0 (May 1997). Supports windows, Windows NT 4.0 SP3, Windows 98 and Windows Me.
- Version 2.0, included with Windows 2000.
- Version 3.0, included with Windows XP (Professional, not Home Edition) and Windows Server 2003.
- Version 4.0, part of Windows Vista and Windows Server 2008.
- Version 5.0, part of Windows 7 and Windows Server R2.
- Version 6.0, part of Windows 8 and Windows Server 2012.
- Version 6.3, part of Windows Server R2.
II. installation of MSMQ
This article MSMQ is deployed in Windows Server R2. Win7/windows Server R2 supported MSMQ feature look at the table:
Feature (Windows 7/windows Server R2) |
Description |
Microsoft Message Queue (MSMQ) Server core/msmq Services |
This is the core service used for sending and receiving messages. |
MSMQ Active Directory Domain Services integration/directory Service Integration |
This feature enables publishing of the queue properties to Active Directory Domain Services (AD DS), the default authentication an D encryption of messages using certificates registered in AD DS, and routing of messages across for Windows sites. MSMQ Active Directory Integration requires the computer to is joined to a domain. |
MSMQ HTTP support/http Support |
This feature enables the sending of messages over hypertext Transfer Protocol (HTTP). MSMQ HTTP support requires that Internet information Services (IIS) is installed on the local computer. |
MSMQ triggers/message Queuing Triggers |
This feature enables the invocation of a Component Object Model (COM) Component or an executable file, depending on the fi Lters that's define for the incoming messages in a given queue. |
MSMQ DCOM Proxy/message Queuing DCOM Proxy |
This feature enables Message Queuing applications to use a MSMQ COM application programming Interface (API) to connect to A remote Message Queuing server. |
Multicasting support/multicasting Support |
This feature supports multicasting messages to a multicast IP address and associating a queue with a multicast IP address. |
Routing Service |
This feature routes messages between different sites and within a site. (note:the Routing Service feature is a available on Windows Server R2.) |
MSMQ is integrated as a functional component in the system, it is very simple to install MSMQ in Windows Server R2, click "Start \ Control Panel \ Administrative Tools \ Server Manager \ functions \ add features \ expand MSM\MSMQ Services". Note: To use the routing service, the server must be joined to a domain.
Third, MSMQ programming development
Before you talk about MSMQ programming, it is important to understand the MSMQ queue first. In MSMQ, queues are used to temporarily store different types of messages. The queues are logically divided into two groups: the application queue and the system queue. Application queues can be created by applications, and system queues are created by MSMQ.
Application queues include Message Queuing, administrative queues, response queues, and report queues.
Message Queuing, applications can communicate through messages in Message Queuing, and applications can send and receive messages through Message Queuing. Note: Lowercase is always displayed in Message Queuing in the computer snap-in after the application creates a queue. However, in MSMQ, the name of the message queue is case-sensitive, so be careful when using the queue name in your code. For example, the application creates a queue named Myqueue, which is displayed as Myqueue in the computer's snap-in. When accessing this queue in an application, the name must be capitalized myqueue, and if you use lowercase myqueue, an exception will be thrown.
Manage queues, in applications that send messages, you can specify an administrative queue, the management queue stores an acknowledgment message sent by MSMQ, and a confirmation message that identifies whether the message sent by the application was successful.
The response queue, the application that sends the message can specify the response queue, and the application that receives the message sends a response message to the response queue.
Report queue, each time the message is passed through the MSMQ routing server, MSMQ sends a report message to track and the report message is stored in the report queue. The report queue is specified and enabled by the sender.
System queues are created by MSMQ or MSMQ administrators, including journal queues and dead-letter queues.
Log queue, when an application queue is created, MSMQ automatically creates a log message that tracks the messages that are being read.
Dead-letter queue, saving messages that were not sent correctly. MSMQ provides two dead-letter queues, a non-transactional dead-letter queue, and a transactional dead-letter queue.
The basic concept of the queue is finished, and the following will begin to talk about the use of concrete. There are a lot of good articles in the garden. The creation of MSMQ Message Queuing, the sending of messages, and the receipt of messages, I will no longer be making wheels again. But in a real-life scenario, if the message is not sent successfully, and the message is very important, each message must be handled, how to use MSMQ, the garden is not a lot of articles introduced, so here I would like to focus on the management queue and dead letter queue usage.
1. New Message sending console application Testack, paste the code:
public class Mynewqueue {static void Main (string[] args) {//Create a new instance of the CL The. Mynewqueue mynewqueue = new Mynewqueue (); Create new queues. Createqueue (". \\private$\\myQueue"); Createqueue (". \\private$\\myAdministrationQueue"); Send messages to a queue. Mynewqueue.sendmessage (); Console.ReadLine (); }//**************************************************//Creates a new queue. public static void Createqueue (string queuepath) { try {if (! Messagequeue.exists (QueuePath)) {messagequeue.create (QueuePath); } else {Console.WriteLine (QueuePath + "already exists."); }} catch (Messagequeueexception e) {Console.WriteLine (e.message); }}//**************************************************//sends a string message to a queue. public void SendMessage () {//Connect to A queue on the local computer. MessageQueue myqueue = new MessageQueue ($ ". \\private$\\myQueue");; Myqueue.label = "Label1"; Create a new message. Message Mymessage = new Message ("Original message"); Mymessage.administrationqueue = new MessageQueue (". \\private$\\myAdministrationQueue"); Mymessage.acknowledgetype = acknowledgetypes.negativereceive; Mymessage.usedeadletterqueue = true; mymessage.timetobereceived = Timespan.fromseconds (2); Mymessage.label = "Label1"; Send the Order to the queue. Thread.Sleep (Timespan.fromseconds (3)); Myqueue.send (Mymessage); Return } }
Mymessage.administrationqueue = new MessageQueue (". \\private$\\myAdministrationQueue");
Specifies the management queue for the message, and the administrative queue is created with the same common message queue.
Mymessage.usedeadletterqueue = true;
Specifies that the message uses a dead-letter queue, and the system sends a copy of the message to the dead-letter queue when the message is sent unsuccessfully.
Mymessage.acknowledgetype = acknowledgetypes.negativereceive;
Specifies that when Message Queuing fails to receive messages, an acknowledgement message is sent to the management queue. Acknowledgetypes is an enumeration type that enumerates values:
Positivearrival |
A mask that is used to request a positive acknowledgment when the original message arrives in the queue. |
Positivereceive |
A mask that is used to request a positive acknowledgment when the original message is successfully retrieved from the queue. |
Negativereceive |
A mask that is used to request a negative acknowledgment when the original message could not be received from the queue. |
None |
A mask that is used to request that no acknowledgment message be sent (either positive or negative). |
Notacknowledgereachqueue |
A mask that is used to request a negative acknowledgment when the original message cannot reach the queue. A negative acknowledgment may be requested when the arrival queue time timer expires or the message cannot be authenticated. |
Notacknowledgereceive |
A mask that is used to request a negative acknowledgment when an error occurs, to prevent receiving the original message from the queue before its receive time timer expires. |
Fullreachqueue |
A mask that is used to request a positive acknowledgment when the original message arrives in the queue, either to request a negative acknowledgment when the queue time timer expires, or to request a negative acknowledgment if the original message cannot be authenticated. |
FullReceive |
A mask that is used to request a positive acknowledgment when the receiving time timer expires before the original message is received from the queue, otherwise a negative acknowledgment is requested. |
Note : The acknowledgement message is a system-generated non-transactional message that identifies whether the message sent by the application was successfully sent to the destination queue or if the message was successfully read by the application.
mymessage.timetobereceived = Timespan.fromseconds (2);
Thread.Sleep (Timespan.fromseconds (3));
Myqueue.send (Mymessage);
A scenario in which Message Queuing failed to receive a message when the impersonation message was sent out. The TimeToBeReceived property of the message specifies the time-out for the queue to receive messages.
After the code executes, view the Message Queuing situation in the computer snap-in, the message queue Myqueue is not receiving a message because it timed out, and the management queue is specified to receive an acknowledgement message when the message is sent unsuccessfully, so an acknowledgment is automatically generated by the system in the management queue; message specifies that a dead-letter queue is used , the system sends a copy of the message to the dead-letter queue at the same time. Such as:
2. New Receive Message console application Ackclient, paste the following code
Class Receiveclient {static void Main (string[] args) {new receiveclient (). Receiveackmessage (); New Receiveclient (). Receivedeadlettermessage (); Console.ReadLine (); } private void Createqueue (string path) {try {if (! Messagequeue.exists (Path)) {messagequeue.create (path); } else {Console.WriteLine ("The queue already exists! "); }} catch (Messagequeueexception ex) {throw; }} private void Receiveackmessage () {MessageQueue myqueue = new MessageQueue (". \\private $\\myadministrationqueue "); MyQueue.MessageReadPropertyFilter.CorrelationId = true; MyQueue.MessageReadPropertyFilter.Acknowledgment = true; Myqueue.formatter = new XMLMessageFormatter (new type[] {typeof (STring)}); MessageQueue mysendqueue = new MessageQueue (". \\private$\\myQueue"); Mysendqueue.formatter = new XMLMessageFormatter (new type[] {typeof (String)}); try {Message mymessage = myqueue.receive (Timespan.fromseconds (3)); Console.WriteLine ("_______________________________"); Console.WriteLine ("Ack Message body:" + myMessage.Body.ToString ()); Console.WriteLine ("Ack Message Id:" + mymessage.id); Console.WriteLine ("Correlation Id:" + Mymessage.correlationid); Console.WriteLine ("Acknowledgment Type:" + myMessage.Acknowledgment.ToString ()); Console.WriteLine ("_______________________________"); } catch (Messagequeueexception ex) {throw; }} private void Receivedeadlettermessage () {MessageQueue myqueue = new MessageQueue ("for Matname:direct=os:.\\system$;D eadletter "); Myqueue.formatter = new XMLMessageFormatter (new type[] {typeof (String)}); MyQueue.MessageReadPropertyFilter.CorrelationId = true; try {Message mymessage = myqueue.receive (Timespan.fromseconds (3)); Console.WriteLine ("__________________________________"); Console.WriteLine ("Dead Letter Id:" + mymessage.id); Console.WriteLine ("Dead Letter Body:" + myMessage.Body.ToString ()); Console.WriteLine ("__________________________________"); } catch (Messagequeueexception ex) {throw; } } }
The Receiveackmessage () method receives the acknowledgement message from the management queue.
Mymessage.correlationid: The Correlationid property of the acknowledgment message identifies the message ID of the original message
Mymessage.acknowledgment: The acknowledgment property of the acknowledgment message identifies the type of acknowledgment message.
The Receivedeadlettermessage () method receives a dead-letter message from the dead-letter queue.
Mymessage.body: Dead Letter message when a copy of the original message, the Body property value of the dead letter message is the same as the Body property value of the original message.
Mymessage.id: The message ID of the dead letter message is the same as the message ID of the original message.
Execution effect
Four, the demo download
Testack.zip
Summary : With the knowledge above, we can handle messages that have not been sent successfully. The solution is to use a dead-letter queue in the program that sends the message, read the dead-letter message from the dead-letter queue in the program that reads the message, and then re-process the dead-letter message.
End : The sample code is to create a private queue natively, which can be performed normally. If you cannot create a queue directly from a remote computer using the creation method, and you cannot use the Exsits method to determine whether the queue exists, my practice is to manually create the queue on the remote computer and then configure read permissions for the queue.
conclusion : About the knowledge point of MSMQ, there are a lot of things to say, such as how to access the queue, the use of the transaction queue, the use of the log queue. If I have time later, I plan to rearrange this article, make a series, and tell each knowledge point. Well, like writing programs, it is impossible to do the first version of the perfect, need an iterative process, constantly to refactor, and constantly to optimize .
Build a LAN test using VMware bridging mode MSMQ (ii)