Message Queue (Microsoft Message Queue) is an asynchronous transmission mode that enables communication between different applications. Applications that communicate with each other can be distributed on the same machine, it can also be distributed anywhere in the connected network space. Its implementation principle is: the Message sender puts the information he wants to send into a container (we call it Message ), then, it is saved to the Message Queue in a public space of the system. local or remote Message receiving programs then extract the messages sent to it from the Queue for processing.
A message queue is a public storage space for sending and receiving messages. It can exist in memory or physical files. Messages can be sent in two ways, express and recoverable. The difference between express and recoverable modes is that the express delivery mode enables fast delivery of messages, messages are stored in the memory instead of on the physical disk to obtain high processing capability. The recoverable mode is in each step of the transfer process, all messages are written to the physical disk for better fault recovery. Message queues can be placed on the sender and receiver, or separately on another machine. It is precisely because of the flexibility of message queues in the placement mode that the reliability of the message transmission mechanism is formed. When the machine that stores the Message Queue fails and restarts, messages sent in recoverable mode can be restored to the status before the fault occurs, and messages sent by express delivery are lost. On the other hand, using the message transmission mechanism, the sender does not need to worry about whether the receiver is started or whether a fault occurs. As long as the message is successfully sent out, it can be considered as completed, in fact, the other party may not even start the instance, or the next day may have actually completed the transaction.
Benefits of using MSMQ
The advantage of MSMQ is that, due to asynchronous communication, the sender and receiver can execute the remaining code without waiting for the other party to return a successful message, therefore, it greatly improves the processing capability of things. In the process of information transmission, the information transmission mechanism has a certain function of fault recovery; MSMQ's message transmission mechanism makes it possible for both parties to communicate with each other to have different physical platforms. Using the MSMQ function provided by Microsoft's. net platform, you can easily create or delete message queues, send or receive messages, and even manage message queues.
Message-to-Column Operations
1. Create a Message Queue
Create a public message queue -- MessageQueue. Create ("MyMachine \ MyQueue ")
Create a Private message queue -- MessageQueue. Create ("MyMachine \ Private $ \ MyPrivateQueue ")
Description: Private $ indicates that a Private message queue is created.
2. Queue reference description
After you create an instance of the MessageQueue component, you must specify the queue to communicate. There are three methods to access a specified message queue:
1) The path is used, and the message queue path is uniquely identified by the machine name and queue name. Therefore, the Message Queue path can be used to specify the message queue used.
2) use format name, which is the unique identifier generated by MSMQ when a message queue is created or after an application is created in the queue.
3) Use a label, which is a descriptive name specified by the queue manager when a message queue is created.
It may not be unique.
3. Use path to reference a queue
Message Queue type |
Path format |
Public queue |
MachineName \ QueueName |
Private queue |
MachineName \ Private $ \ QueueName |
Journal queue |
MachineName \ QueueName \ Journal $ |
Machine journal queue |
MachineName \ Journal $ |
Machine dead letter queue |
MachineName \ Deadletter $ |
Machine transactional dead letter queue |
MachineName \ XactDeadletter $ |
The Message Queue Server parses the path and format name (format name) when receiving an operation request using the Message Queue path ), therefore, the queue is not as efficient as the format name method.
When a message queue is not connected, you can only send messages to it using the format name.
In addition to the path attribute, the reference of a path name can also be obtained from the MachineName and QueueName attributes.
Example of path reference:
MessageQueue1.path = ". \ MyQueue"
The format name is automatically generated by the queue manager when the queue is created.
. When your component is used as a part of a WEB sevice or WEB call, it is best to reference the queue by format name because it is faster.
. When sending a message to a non-connected queue, the format name should be used, because path resolution will fail when the queue is not connected.
. When the network topology changes or the message queue is rebuilt, the format name changes.
In delphi, MSMQ is used.
Open Control Panel-> Program-> Add component and add Message Queue
Open the control panel-> Computer Management-> services and applications-> message queue. There are two types of message pair columns: private and public. Private Messages can only be used if they are authenticated by the other party, public is not required. Add Test.
Add MSMQ controls, TMSMQMessage, TMSMQQueueInfo, TMSMQQueue, and TMSMQEvent in Delphi. These controls exist in Project-> Import type Library.
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, msmq_tlb, comobj, stdctrls, oleserver; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); Procedure formdestroy (Sender: tobject); Procedure alert (Sender: tobject); Private msmqmessage: tmsmqmessage; Response: Response; Response: response; Msmqevent: tmsmqevent; // values: msmqtransaction; values: dependencies; {private Declarations} procedure messages (Sender: tobject; const queue: idispatch; cursor: integer); public procedure msmqpostmessage; procedure msmqreceivemessage; {public declarations} end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.button1click (Sender: TOB Ject); begin msmqpostmessage; end; Procedure tform1.formdestroy (Sender: tobject); begin msmqmessage. free; msmqqueueinfo. free; msmqqueue. free; msmqevent. free; msmqtransactiondispenser. free; //: tmsmqtransactiondispenser; end; //////////////////////////////////////// //////////////////////////////////////// // function: asynchronous Method for handling received emails // parameters: //////////////////////////////////////// //////////////////////////////////////// P Rocedure tform1.msmqeventarrived (Sender: tobject; const queue: idispatch; cursor: integer); var MSG: variant; begin MSG: = msmqqueue. receive_v1 ();//. receive; end; //////////////////////////////////////// //////////////////////////////////////// // function: put the incoming message into the message queue. First, check whether the message queue exists. If not, create the // parameter: //////////////////////////////////////// //////////////////////////////////////// procedure tform1.msmqpostmessage; VA R strbody: string; istransactional: olevariant; msmqtransactionact: msmqtransaction; msmqtransaction: tmsmqtransaction; begin istransactional: = true; msmqtransaction: = tmsmqtransaction. create (NiL); msmqqueueinfo. pathname: = '. \ private $ \ test4 '; msmqqueueinfo. remotemachinename: = 'shangjin'; // or you can change it to the computer name msmqqueueinfo. istransactional; try msmqqueueinfo. open (mq_send_access, mq_deny_none); doesn t o N e: eoleexception do begin if E. Message = 'the queue does not exist, or you do not have sufficient permissions to perform this operation. 'Then begin msmqqueueinfo. defaultinterface. label _: = 'te'; msmqqueueinfo. pathname: = '. \ private $ \ test7'; msmqqueueinfo. remotemachinename: = 'shangjin'; istransactional: = true; msmqqueueinfo. defaultinterface. create (istransactional, emptyparam); End else raise; end; strbody: = '2013'; (msmqmessage. defaultinterface as imsmqmessage ). body: = strbody; msmqtransactionact: = msmqtransactiondispenser. Begintransaction; msmqqueue. connectto (msmqqueueinfo. open (mq_send_access, mq_deny_none); msmqmessage. send (msmqqueueinfo. open (mq_send_access, mq_deny_none); end; //////////////////////////////////////// //////////////////////////////////////// // function: retrieving a message from a message queue is not used yet. // parameter: // remarks: only when the message queue application knows the complete path name, direct format name, or private format name of the queue can access the dedicated queue. // path name: computername \ private $ \ queuename. // The path name on the Local Computer: \ private $ \ queuename. // Direct format name: Direct = computeraddress \ private $ \ privatequeuename. // Private format name: Private = computerguid \ queuenumber. // You must set the permission before using it. // For the Message Queue application on the local computer to access the dedicated queue, this application only needs to know the path name of the // queue. For a Message Queue application on a remote computer, the application needs to know the queue's // direct or dedicated format name to access this queue. // The dedicated format name can be msmqqueue. queueinfo. get the formatname ////////////////////////////////////// //////////////////////////////////////// // procedure tform1.msmq1_emessage; begin msmqqueueinfo. pathname: = 'shangjin \ private $ \ test'; msmqqueue. disconnect; msmqqueue. connectto (msmqqueueinfo. open (mq_receive_access, mq_deny_none); msmqevent. onarrived: = msmqeventarrived; msmqqueue. enablenotification (msmqevent. defaultinterface); end; Procedure tform1.formcreate (Sender: tobject); begin msmqmessage: = tmsmqmessage. create (NiL); msmqqueueinfo: = tmsmqqueueinfo. create (NiL); msmqqueue: = tmsmqqueue. create (NiL); msmqevent: = tmsmqevent. create (NiL); msmqtransactiondispenser: = tmsmqtransactiondispenser. create (NiL); end.