Application of Message queue in the development of vb.net database

Source: Internet
Author: User
Tags message queue client msmq
Data | Database Let's start with a quick look at what is Message Queuing (MSMQ)? Message Queuing is the basis for communication in Windows MSMQ,WIN95/98/ME/XP, which does not include Message Queuing services but supports clients, and is also used to create distributed, loosely connected communication applications. These applications can communicate through different kinds of networks, or with computers that are offline. Message queues are divided into user-created queues and system queues, and user queues are divided into:

· Public queues are replicated and transmitted throughout the Message Queuing network of transitive messages, and are potentially accessible by all sites connected to the network.

· Private queues are not published throughout the network. Instead, they are available only on the local computer where they reside. A private queue can only be accessed by an application that knows the full pathname or label of the queue.

· The management queue contains messages confirming receipt receipts for messages sent in a given Message Queuing network. Specify the administrative queues that you want the MessageQueue component to use

· Response queues contain response messages that are returned to the sending application when a message is received by the target application. Specifies the response queue that you want the MessageQueue component to use.

System queues are divided into:

· Journal queues Optionally store copies of messages that are sent and those that are removed from the queue.

· The dead-letter queue stores a copy of a message that cannot be delivered or has expired.

· The private system queue is a series of private queues for the management and notification messages required by the storage system to perform message processing operations.

Now that you have a simple understanding of Message Queuing, it's time to go to the topic. To use MSMQ for software development, you need to install MSMQ. After installation, it is time to enter the actual development phase. Open the Service Explorer in the Vs.net IDE first, expand the computer name of the message queue you want to establish, and then expand Message Queuing to right-click it to create a new message queue by selecting "New" in the pop-up menu and assigning it a name, which is optional. It can also be done programmatically, with the following code:

System. Messaging.MessageQueue.Create (". \private$\myprivatequeue") ' establishes a private queue
System.Messaging.MessageQueue.Create ("Mymachine\myqueue") ' establishes a public queue

In fact, I don't think it's important to use that approach, it's important to figure out the difference between a private queue and a public queue (other queues are not necessary). In this case, private queues and public queues are established on the server, respectively, through Server Explorer.

Program Features: This program is divided into two parts, including server programs (installed on the SQL Server server) and client programs, the role of the client is to write T-SQL statements and put T-SQL statements in the message, and send messages to the SQL Server server in the message queue. The server program checks the specified message queue to begin executing the contents of the message when a new message arrives, because the contents of the message are T-SQL statements, so the server side is actually performing operations on the database.

Client program:


Public Sub Client ()
Dim TM as New System.Messaging.MessageQueue ()
Tm.path = ". \private$\jk" ' "formatname:public=3d3dc813-c555-4fd3-8ce0-79d5b45e0d75" to establish a connection to Message Queuing in the specified computer.
Dim Newmessage as New System.Messaging.Message (TextBox1.Text) ' t-SQL statement accepting text baskets
Newmessage.label = "This is the Label" ' Message name,
Tm.send (newmessage) ' Send Message
End Sub

Service-Side programs:


Public Sub Server ()
Dim Newqueue as New System.Messaging.MessageQueue (". \private$\jk") ' formatname:public= 3d3dc813-c555-4fd3-8ce0-79d5b45e0d75 "' establishes a connection to a message queue in the specified computer,
Dim m as System.Messaging.Message
' View messages in message queues
m = newqueue.receive
M.formatter = new System.Messaging.XmlMessageFormatter (new String () {"System.string,mscorlib"})
Dim St as String
st = M.body ' message content for messages in queue. Both SQL statements
Dim con as New oledb.oledbconnection ("Enter your own database connection string")
Con. Open ()
Dim com as New oledb.oledbcommand (St, con) ' Execute the SQL statement in the message
Com. ExecuteNonQuery ()
Con. Close ()
End Sub

Why do I have to use Message Queuing to handle the operation of the database I have not answered the question, and now I am going to answer that question. In this program you will find that in sub-client () I do not connect to the database and request data, but by sending messages to manipulate the database, this benefit is to save two parts of time:

1, to the database to solve the request data time.

2, the time to return data 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 a John record, you can put a simple DELETE statement into the message and send it to the server for the server program to handle changes to the data.

Another major use of Message Queuing, which is essential in current ERP software, is to save information when disconnected and send messages when the connection is restored. Messages cannot be passed quickly to their queues in the following two cases: when the computer where the queue resides is not working, or when the domain controller required to route the message is not working. Message Queuing allows you to respond to situations where you can continue to send messages when you disconnect from the network or if the necessary computer or controller is not working. In these cases, the message is temporarily stored in the queue of a computer on the local computer or delivery route until the resource required to complete delivery is brought back online.

For example, suppose you have a central queue that records all orders sent by a salesperson on a business trip. These salespeople work most of the day disconnected, record order information from the customer's site, and dial the connection once a day to transfer all of that information to the central queue. Because messages can still be sent to the queue when the sender disconnects, the salesperson can send their message as soon as the customer information is logged, but the system caches the messages until a dial-up connection is made at night.

How do I save a message when I disconnect? Sending a message to a disconnected queue is almost identical to sending a message to an available queue. When the queue that you want to send to is not available, you do not have to make any special configuration to have the component store messages in a temporary queue. There is an annotation statement after the Tm.path = ". \private$\jk" of the client code, which is actually the ability to send messages to disconnected queues. Simply replace the Tm.path = ". \private$\jk" statement with Tm.path = "formatname:public=3d3dc813-c555-4fd3-8ce0-79d5b45e0d75" The number after public is the GUID number to send to the computer. This number can open the properties of the message queue of that computer see. Using this method, you can ensure that the operation of the server is valid when disconnected. Now when you run this program, open the "Start"-"program"-"Management Tools"-"Computer Management" in Win2000. In the Computer Management window, expand Services and Applications-"Message Queuing"-"outgoing queues", and you will see the messages you have created in the right window. (If you use the Tm.path = ". \private$\jk" statement, expand the services and applications-"Message Queuing"-"private Queues" in the Computer Management window to see the queues you set up.) )

In fact, Message Queuing programming is not complicated, but it is very useful in the development of the network environment, can simplify a lot of development process and save development time.

In fact, Message Queuing programming has a lot of flexibility, can almost solve the network programming most of the problems. such as chat program, remote control program.

This article is a simple introduction to Message Queuing, and an example to illustrate how to use message programming under. NET to achieve fast, efficient and stable operation of the database. The last thing to add is that you can use Message Queuing on the internet as well, just tm.path = "formatname:public= The number following the 3D3DC813-C555-4FD3-8CE0-79D5B45E0D75 statement becomes the number on the server where the message queue resides. But to remind you that using messages will take up a lot of bandwidth when you are transmitting, you should not use messages when you are not required to do programming under the Internet.

Passed under VB.net, Win2000, 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.