Message queue programming (transfer)

xiaoxiao2021-03-06  62

Let's take a simple understanding what is the message queue (MSMQ)? The message queue is Windows 2000 (NT also has MSMQ, WIN95 / 98 / ME / XP does not include message queue services but supports the client's running) Operating system, which is used to create a distributed, loose connection communication application tool. These applications can communicate through different kinds of networks, or with offline computer. The message queue is divided into user creation queues and system queues, and the user queue is divided into:

· "Public Square" copies and transmits throughout the "Message Queue" network that can be delivered, and there is a possibility of access to all sites connected by the network.

· "Special queue" is not released throughout the network. Instead, they are only available on the resident local computer. A dedicated queue can only be accessed by an application that knows the full path name or tag of the queue.

· The "Management Queen" contains messages that confirm the message receipt of the "Message Queue" network. Specify the management queue you want to use by the MessageQueUE component

• Response Queue contains a response message to send an application when the target application receives a message. Specifies the response queue you want to use by the MessageQue component.

The system queue is divided into:

· "Diary Queue" optionally stores a copy of the send message and a copy of the message removed from the queue.

· "Dead Letter" stores a copy that cannot be passed or expired.

· "Special System Query" is a special queue for the management and notification messages required for the execution of the message processing operation.

Now everyone has a simple understanding of the message queue, it will enter the theme. To use MSMQ for software development, you need to install MSMQ. This will enter the actual development phase after installation. Open the "Service Source Manager" in the VS.NET IDE to expand the computer name you want to create a message queue, and then expand the "Message Queue" right-click it in the pop-up menu to select "New" to create a new message queue, and Specify a name for it, this name can be casual. You can also be done by programming, the code is as follows:

System.Messaging.MessageQueue.create ("./ private $ / myprivatequeue") 'Establish a dedicated queue

System.Messaging.MessageQueue.create ("MyMachine / MyQueue") 'Establishing a public queue

In fact, I don't think it is not important to use the method, it is important to figure out the difference in special queues and public queues (other queues are not required). In this example, a dedicated queue and public queue is established on the server through "Server Explorer".

Program function: This program is divided into two parts including server programs (installed on the SQL Server server) and client programs, the client's role is used to write T-SQL statements and put the T-SQL statement in the message and will mess up Send it to the message queue on the SQL Server server. The server program checks the specified message queue when there is a new message to arrive, the content in the message is started. Since the content in the message is the T-SQL statement, the server side is actually the operation of the database.

Client program:

Public Sub Client () DIMTM AS New System.MESSAGING.MESSAGEQUE () TM.PATH = "" FormatName: public = 3d3dc813-C5555-4FD3-8CE0-79D5B45E0D75 "'is in the specified computer Message Queue Establishment, DIM NewMessage As New System.Messaging.Message (TextBox1.text) 'Accept the T-SQL Statement NewMessage.label = "this is the label" message name, TM.send (newMessage)' Message END SUB Server Program:

Public Sub Server () Dim NewQueue As New System.Messaging.MessageQueue ("./ Private $ / JK") '"FormatName: Public = 3D3DC813-C555-4FD3-8CE0-79D5B45E0D75"' establishes a connection with message queues in the specified computer , Dim m As System.Messaging.Message 'view the message in the message queue m = NewQueue.Receive m.Formatter = New System.Messaging.XmlMessageFormatter (New String () { "System.String, mscorlib"}) Dim st As Stringst = The message content of the message in the M. Body 'Message queue. SQL statement DIM Con AS New OLEDB.OLDBCONNECTION ("Enter your own database connection string") Con-() DIM COM AS New OLEDBCOMMAND (ST, CON) COM.EXECUTENONQUERY () Con. close () End Sub

Why do I use a message queue to handle the operation of the database, I have never answered, now I will answer this question. In this program, you will find that I didn't connect the database and request data in Sub Client (), but by sending a message to manipulate the database, this advantage is to save two parties: 1. Request data for database decomposition time. 2, return the time from the database to the data. In many cases, in fact, we don't need to see the specific data, know how to modify the data in the database. For example, you want to delete a record, you can put a simple delete statement into the message and send the server to process the server program to change the data. Another major use of the message queue is that the current ERP software is essential, which is to save the information when the connection is turned off, and the message is sent when the connection is restored. The message cannot be quickly passed to their queues in the following two cases: When the queue resides, it cannot work, or when the routing message needs to work. "Message Queue" allows you to deal with these situations such that you can continue to send messages when you disconnect the connection or the necessary computer or controller from the network. In these cases, the message is temporarily stored in a queue of a computer on a local computer or passing the route until the resource required to pass the transfer is completed. For example, suppose there is a central queue that records all orders sent by the salesperson of the business trip. These salesperson works most of the time of disconnection, records order information from the customer site, and dials every day, transfer all of this information to the central queue. Because the message can still be sent to the queue when the sender is open, the salesperson can send their messages immediately when recording customer information, but the system can cache these messages until the dial-up connection is performed. How to save the message when disconnecting? The process of sending messages to the disconnected queue is almost identical to the process of sending messages to the available queue. When the queue to which you want to send, you don't have to make any special configuration to store the message in the temporary queue. There is a comment statement behind the TM.Path = "./private there //jk", in fact, this statement is the functionality that implements the message to the disconnected queue. As long as TM.PATH = "./private $/jk" is changed to TM.Path = "FormatName: public = 3D3DC813-C555-4FD3-8CE0-79D5B45E0D75" where the number behind PUBLIC is the GUID to be sent to the computer digital. This number can open the properties of the message queue of that computer. Using this method can ensure that the operation of the server is valid in the case where the connection is turned off. After running this program now, open the "Start" - "" "" Administrative Tool "-" Computer Management "in Win2000. In the Computer Management window, "-" "" - "" "" "" "" "" "" "" "" "" "" "" "-", you will see the message you created in the window. (If you use TM.Path = "./private!" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "- In fact, the programming of the message queue is not complex, but it is very useful in the development of the network environment, which simplifies a large number of development processes and saves development time. In fact, the programming of the message queue has a lot of flexibility, and most of the problems can be solved in network programming. For example, chat programs, remote control programs. This article makes a simple introduction to the message queue and gives an example to explain how to use message programming under the .NET to achieve fast and efficient and stable database operations.

转载请注明原文地址:https://www.9cbs.com/read-84189.html

New Post(0)