Application of message queue in VB. NET database development

Source: Internet
Author: User
Tags msmq
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:

1. the "Public queue" is replicated and transmitted throughout the "message queue" network where messages can be transmitted, and may be accessed by all sites connected to the network.

2. "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.

3. "Management queue" contains the message that confirms the receipt of messages sent in the given "message queue" network. Specifies the management queue to be used by the MessageQueue component.

4. The "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:

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

2. "Dead message queue" stores copies of messages that cannot be passed or have expired.

3. "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 vs.net 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 ("myMachineMyQueue") '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) 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. 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 ())
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. Change tM. Path = ". Private $ jk" to 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.

Passed in vb.net, win2000, and SQL server 2000.

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: info-contact@alibabacloud.com 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.