Concept of Message Queue

Source: Internet
Author: User
Tags msmq

I. Main concepts in Message Processing

A message is a data unit transmitted between two computers. Messages can be very simple, for example, containing only text strings, or more complex, and may contain embedded objects.

The message is sent to the queue. "Message Queue" is the container that stores messages during message transmission. The message queue manager acts as a man-in-the-middle when a message is relayed from its source to its target. The main purpose of a queue is to provide a route and ensure message transmission. If the receiver is unavailable when a message is sent, the Message Queue retains the message until it can be passed successfully.

"Message Queue" is Microsoft's message processing technology. It provides message processing and Message Queue functions for any application in any computer combination installed with Microsoft Windows, whether these computers are on the same network or online at the same time.

The message queue network is a group of computers that can send messages to and from each other. Different computers in the Network play different roles in ensuring smooth message processing. Some of them provide routing information to determine how to send messages, some store important information of the entire network, and some only send and receive messages.

During the "message queue" installation, the Administrator determines which servers can communicate with each other and sets special roles for specific servers. Computers that make up the message queue network are called sites, which are connected to each other through site links. Each site link has an associated "overhead", which is determined by the Administrator, indicating the frequency of message passing through the site link.

The "message queue" Administrator also sets one or more computers on the network as the "Routing Server. The routing server can view the overhead of links of different sites and determine the fastest and most effective way to transmit messages through multiple sites to determine how to transmit messages.

Ii. Queue type

There are two main types of Queues: queues created by you or other users in the network and system queues. A user-created queue may be any of the following Queues:

The "Public queue" is replicated across the "message queue" network and may be accessed by all sites connected to the network.

"Dedicated queue" is not released across the network. Instead, they are only available on the local computer where they reside. A dedicated queue can only be accessed by applications that know the full path name or tag of the queue.

"Management queue" contains messages that confirm the receipt of messages sent in a given "message queue" network. Specifies the management Queue (if any) to be used by the messagequeue component ).

The "response queue" contains the response message returned to the sending application when the target application receives the message. Specify the response queue (if any) to be used by the messagequeue component ).


The queues generated by the system are generally divided into the following types:

The "Diary queue" can be used to store the copy of the sent message and the copy of the removed message from the queue. A single diary queue on each "message queue" client stores copies of messages sent from this computer. A separate diary queue is created for each queue on the server. This diary tracks messages removed from this queue.

"Dead-letter queue" stores copies of messages that cannot be passed or have expired. If an expired message or a message that cannot be delivered is a transactional message, it is stored in a special dead-letter queue, called a "transactional dead-letter queue ". Dead messages are stored on the computer where the expired messages are located.

The "Report queue" contains the message indicating that the message is routed to the target, and it can also contain the Test message. Each computer can have only one report queue.

"Dedicated system queue" is a dedicated queue for managing and notifying messages required by a series of storage systems to perform message processing operations.

Most of the work done in applications involves accessing public queues and their messages. However, several different system queues may be used in daily operations based on the application's logging, validation, and other special processing needs.

3. synchronous and asynchronous communication

Queue communication is inherently asynchronous, because sending messages to the queue and receiving messages from the queue are completed in different processes. In addition, the receiving operation can be performed asynchronously, because the recipient can call the beginreceive Method for any given queue, and then immediately continue other tasks without waiting for a response. This is quite different from what people know about "synchronous communication.

In synchronous communication, the request sender must wait for a response from the specified receiver before executing other tasks. The sender's waiting time depends entirely on the time used by the receiver to process the request and send the response.

Iv. Message Security

You can use the following methods to protect the content of the sent and received messages:

You can use authentication to authenticate the source of the message received by the application.

You can use encryption to prevent unauthorized users from reading or using your messages.

You can use ACL-based access control permissions to restrict users and use code access security to restrict code from sending or reading messages to or from a specific queue on a computer.

Users who attempt to access the "message queue" object, the Operation Type of the attempt, and whether the access is successful or failed.

5. Install "message queue"

To use message queue in code or server resource manager, you must install "message queue" on the development computer ". In Windows 2000 or Windows NT, you can find instructions on how to install message queue. During installation, you must specify the role of the new master "message queue" server being created: act as the hub of the new network, the independent client of the existing network, or the dependent client of the existing network. In most cases, you do not need to create a new network, but can join the existing "message queue" Network of the Company or working group.

Install Message Queues with independent client functions

Open the add or delete program.

Click Add/delete Windows Components ".

In the Windows component wizard, select the Message Queue check box.

To change the optional sub-component set installed by default, click Details and perform the following operations.

If the computer belongs to a domain and you want it to run in workgroup mode, clear the "Active Directory integration" check box.

If you do not want to install a Message Queue trigger, clear the "Trigger" check box.

If you want to send messages by the HTTP transmitter, select the "msmq http support" check box.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------

First, let's take a look at what is MSMQ )? Message Queue is the basis for communication in Windows 2000 (NT also has MSMQ, Win95/98/ME/XP does not include Message Queue Service but supports running of clients, it is also a tool used to create distributed, loose connection communication applications. These applications can communicate with offline computers through different types of networks. Message Queues are divided into user-created queues and system queues. User queues are divided:

· "Public queues" are replicated and transmitted across the entire "message queue" network that can transmit messages, and may be accessed by all sites connected to the network.

· "Dedicated queue" is not released throughout the network. Instead, they are only available on the local computer where they reside. A dedicated queue can only be accessed by applications that know the full path name or tag of the queue.

· "Management queue" contains messages that confirm receipt of messages sent in a given "message queue" network. Specifies the management queue to be used by the messagequeue component

· "Response queue" contains the response message returned to the sending application when the target application receives the message. Specify the response queue to be used by the messagequeue component.

System queues are divided:

· The "Diary queue" can be used to store copies of messages sent and removed from the queue.

· "Dead-letter queue" stores copies of messages that cannot be passed or have expired.

· "Dedicated system queue" is a dedicated queue for managing and notifying messages required by a series of storage systems to perform message processing operations.

Now that you have a simple understanding of message queues, you should enter the topic. To use MSMQ for software development, you must install MSMQ. After installation, you should enter the actual development stage. Open "service start Resource Manager" in ide to expand the name of the computer on which you want to create a message queue, expand "message queue", right-click it, and select "new" in the pop-up menu to create a new message queue and specify a name for it. This name can be used at will. You can also program the Code as follows:

System. messaging. messagequeue. Create (". \ private $ \ myprivatequeue") 'create a dedicated queue

System. messaging. messagequeue. Create ("mymachine \ myqueue") 'creates a public queue

In fact, I don't think it is important to use this method. It is important to clarify the difference between a dedicated queue and a public Queue (other queues are not required ). In this example, a dedicated queue and a public queue are created on the server through "server resource manager.

Program function: This program is divided into two parts: the server program (installed on the SQL Server server) and the client program, the client is used to compile the T-SQL statement, place the T-SQL statement in the message, and send the message to the message queue on the SQL Server server. The server program checks the specified message queue. When a new message arrives, it starts to execute the content in the message, because the message content is a T-SQL statement, the server actually performs database operations.

Client Program:

Public sub client ()

Dim TM as new system. messaging. messagequeue ()

TM. Path = ". \ private $ \ JK" '"formatname: Public = 3d3dc813-c555-4fd3-8ce0-79d5b45e0d75"' establishes a connection with the message queue on the specified computer,

Dim newmessage as new system. messaging. Message (textbox1.text) 'Accept the T-SQL statement of the text basket

Newmessage. Label = "this is the label" 'message name,

TM. Send (newmessage) 'sends a message

End sub

Server program:

Public sub server ()

Dim newqueue as new system. messaging. messagequeue (". \ private $ \ JK") '"formatname: Public = 3d3dc813-c555-4fd3-8ce0-79d5b45e0d75"' connects to the message queue on the specified computer,

Dim M as system. messaging. Message

'View messages in a message queue

M = newqueue. Receive

M. formatter = new system. messaging. xmlmessageformatter (new string () {"system. String, mscorlib "})

Dim st as string

St = M. Body 'message content of a message in the message queue. Both SQL statements

Dim con as new oledb. oledbconnection ("Enter your database connection string ")

Con. open ()

Dim com as new oledb. oledbcommand (St, con) 'executes the SQL statement in the message

Com. executenonquery ()

Con. Close ()

End sub

I have never answered the question why I want to use message queue to handle database operations. Now I will answer this question. In this program, you will find that in sub client (), I did not connect to the database and request data, but operated the database by sending messages. The advantage is that it saves two parts of time:

1. the time when the database is connected to the request data.

2. Time when data is returned from the database.

In many cases, we do not need to see the specific data to know how to modify the data in the database. For example, to delete Michael's record, you can put a simple Delete statement into the message and send it to the server for the server program to process the data changes.

In addition, another major purpose of message queue is that the current ERP software is essential, that is, to save the information when the connection is closed and send messages when the connection is restored. Messages cannot be quickly transmitted to their queues in either of the following situations: when the computer where the queue resides cannot work, or when the domain controller required to route messages cannot work. Message Queue allows you to continue sending messages when you disconnect from the network or the necessary computer or controller cannot work. In these cases, messages are temporarily stored in the queue of a local computer or a computer on the transfer route until the resources required for the transfer are re-Online.

For example, assume that there is a central queue that records all orders sent by sales personnel on a business trip. These sales staff work in disconnected mode most of the day, record order information from the customer's site, and dial-up connection once a day to transfer all this information to the central queue. Because messages can still be sent to the queue when the sender disconnects, the sales staff can send their messages immediately when Recording customer information, but the system will cache these messages until the dial-up connection is established in the evening.

How do I save messages when the connection is disconnected? The process of sending messages to a disconnected queue is almost identical to that of an available queue. When the queue to be sent to is unavailable, no special configuration is required for the component to store messages in the temporary queue. There is a comment statement after TM. Path = ". \ private $ \ JK" in the client code. In fact, this statement is used to send messages to the disconnected queue. As long as the TM. path = ". \ private $ \ JK "is replaced with TM. path = "formatname: Public = 3d3dc813-c555-4fd3-8ce0-79d5b45e0d75" where the number after public is the guid number to be sent to the computer. You can view the Message Queue attribute of the computer. By using this method, you can ensure that the operation on the server is effective when the connection is disconnected. After running this program, open "start"-"program"-"Management Tools"-"Computer Management" in Win2000 ". In the "Computer Management" window, expand "services and applications"-"message queue"-"outgoing queue". You will see the created message in the window on the right. (If you use TM. path = ". \ private $ \ JK "statement, in the "Computer Management" window, expand "services and applications"-"message queue"-"dedicated queue" to view your created queue .)

In fact, Message Queue programming is not complex, but it is very useful in program development in the network environment, which can simplify a lot of development processes and save development time.

In fact, Message Queue programming has great flexibility, and almost solves most network programming problems. Such as chat programs and remote control programs.

This article gives a brief introduction to message queues. It also provides an example to illustrate how to use message programming in. Net to quickly, efficiently, and stably operate databases. In the end, you can also use message queue on the Internet. path = "formatname: Public = 3d3dc813-c555-4fd3-8ce0-79d5b45e0d75 the number after the statement is changed to the number of the server where the message queue is located. However, it is important to note that messages will occupy a large amount of bandwidth during transmission, so when it is not necessary, do not use messages for programming on the Internet.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.