Understand MSMQ and easily control ASP Processes

Source: Internet
Author: User

What exactly is MSMQ? Let's take a look at the following:

1. Basic Introduction to Microsoft Message Queue:

MSMQ (also known as "falcon") is a service running on Windows NT. It provides asynchronous communication between applications. You can find it in NT4 Option Pack. The basic concept of MSMQ is very simple: it can be seen as an email between applications: a message is packaged into a specific type of container, and save the message to a queue with special functions until the recipient accepts the message. These queues ensure MSMQ transmission regardless of the current network connection status.

Like all emails, MSMQ messages have a sender and a receiver, and the receiver should be able to access the queue. A single message in a single queue, which has multiple recipients, such as respinder. The message sender is usually Web Server (IIS ).

MSMQ can also communicate with other message systems. For example, Sun Solaris, HP-UNIX, OS/2, VMS, AS/400 platform. Like other BackOffice services, MSMQ has a com api (mqoa. dll) provided to developers. The three most common classes are msmqqueueinfo, msmqqueue, and msmqmessage.

(1) msqmqueueinfo

Msmqqueueinfo allows you to create, open, and delete messages in the queue. To establish a contact with the queue, you must first set pathname. This is a queue name attribute, which tells msqm which machine is in the queue.
<%
Dim objqueueinfo
Dim objqueue
Set objqueueinfo = server. Createobject ("MSMQ. msmqqueueinfo ")
Objqueue. pathname = "./myqu"
Set objqueue = objqueueinfo. Open (mq_send_access, mq_deny_none)
%>

The above code opens a local queue named myqueue. If the queue is on another server, the code should be as follows:

Objqueue. pathname = "someothercomputer/myqu"

There are two parameters in the open queue: access and unlock mode. Access indicates what operations will be performed on the queue. Generally, there are three operations:

Mq_peek_access (32), mq_receive_access (1), mq_send_access (2 ).

Mq_peek_access is used to find messages in a specific queue. However, this message is not operated.

Mq_receive_access is used to delete a message in the queue after it is read.

Mq_send_access is used to send messages in the queue but does not receive messages.

Note that an msmqqueue object is returned after the open operation is used. The following is a typical example of new and delete operations:
<%
Dim objqueue
Set objqueue = server. Createobject ("MSMQ. msmqqueueinfo ")
Objqueue. pathname = "./myqu"
Objqueue. Create
%>

<%
Dim objqueue
Set objqueue = server. Createobject ("MSMQ. msmqqueueinfo ")
Objqueue. pathname = "./myqu"
Objqueue. Delete
%>

(2) msmqqueue

The msmqqueue class is used to describe a queue opened in the MSMQ Service. This class provides a function to loop messages in the pointer queue. You cannot open a queue that uses the msmqqueue class. To do this, you can only use msqmqueueinfo (see the preceding example). Although many ASP applications usually use MSMQ to send messages, however, ASP is often required to display the specific content of the message.

There are two ways to get messages: synchronous and asynchronous, but ASP can only use synchronous. This is because ASP cannot declare a withevents variable on the server.

Here is an example of using MSMQ asynchronously (only in VB)
Option explicit
Dim m_objqueueinfo as new msmqqueueinfo
Dim m_objqueue as msmqqueue
Dim withevents m_objmsmqevent as msmqevent

Private sub form_load ()
M_objqueueinfo.pathname = "./myqu"
M_objqueueinfo.label = "my sample queue"
On Error resume next
M_objqueueinfo.create
On Error goto 0
Set m_objqueue = m_objqueueinfo.open (mq_receive_access, mq_deny_none)

Set m_objmsmqevent = new msmqevent
M_objqueue.enablenotification m_objmsmqevent, mqmsg_current, 1000
End sub

Private sub m_objmsmqevent_arrived (byval queue as object, byval cursor as long)
Dim m_objmessage as msmqmessage
Set m_objmessage = queue. peekcurrent
Msgbox "Message received ed:" & m_objmessage.label
M_objqueue.enablenotification m_objmsmqevent, mqmsg_next, 10000
End sub

Private sub m_objmsmqevent_arrivederror (byval queue as object, byval errorcode as long, byval cursor as long)
Msgbox "error accorded:" & errorcode
End sub

This code first creates a queue (if it does not exist ). Then the m_objmsmqevent object is connected to the msmqqueue object by calling enablenotification. Once you connect to the msmqevent object, you only need to complete the arrived and arrived_error (optional) events. The arrived event is triggered when a new message arrives in the queue. This event returns two pointers. One is to the position where the message should be read from the queue, and the other is the current position. If an error occurs, the arrivederror event is triggered. When a message is synchronously obtained, the program will not be suspended until the message is available or timed out. The Code is as follows:
Public sub displaymessages ()
Dim objqueueinfo as new msmqqueueinfo
Dim objqueue as msmqqueue
Dim objmessage as msmqmessage
Objqueueinfo. pathname = "./myqu"
Objqueueinfo. Label = "my sample queue"

On Error resume next
Objqueueinfo. Create
On Error goto 0
Set objqueue = objqueueinfo. Open (mq_receive_access, mq_deny_none)
Do While true
Set objmessage = objqueue. Peek (, 1000)
If objmessage is nothing then exit do
Msgbox "message:" & objmessage. Label
Loop
Msgbox "no more new messages ."
Objqueue. Close
Set objmessage = nothing
Set objqueue = nothing
Set objqueueinfo = nothing
End sub

(3) msmqmessage

The msmqmessage class supports all attributes of messages in a queue. MSMQ messages have two methods and a wide range of attributes. The two main attributes are body and label. The most important method is send. There are two methods to obtain messages: Opening and peeking. When opening is used, the message is deleted. When peeking is used, the message is still saved in the queue until it expires. Their return values are pointer to the message. The Code in the following example opens a message and displays its body and label
Private sub lookformessage ()
Dim objqinfo as new msmqqueueinfo
Dim objqreceive as msmqqueue
Dim objmessage as msmqmessage
Objqinfo. pathname = "./test"
Set objqreceive = objqinfo. Open (mq_receive_access, mq_deny_none)
Set objmessage = objqreceive. Receive (, 1000)
If not objmessage is nothing then
Msgbox objmessage. Label & "-" & objmessage. Body
Else
Msgbox "Nothing in the queue"
End if
Objqreceive. Close
Set objqinfo = nothing
Set objqreceive = nothing
Set objmessage = nothing
End sub

This code opens a queue and searches for messages in the queue. The receive method is used to set a 1000 microsecond timeout, it tells msmq1000 microseconds to stop searching and set a timeout period. It is mainly used to check whether a message exists rather than waiting for a message. That is to say, if you want to know whether there is a message, you can use this method. If no message exists, the returned pointer is null (if not objmessage is nothing ). The following code sends a message:
<%
Dim objqinfo
Dim objqsend
Dim objmessage
Set objqinfo = server. Createobject ("MSMQ. msmqqueueinfo ")
Objqinfo. pathname = "./test"
Set objqsend = objqinfo. Open (mq_send_access, mq_deny_none)
Set objmessage = server. Createobject ("MSMQ. msmqmessage ")
Objmessage. Label = "this is the label ."
Objmessage. Body = "this is the body ."
Objmessage. Send objqsend
Objqsend. Close
Set objqinfo = nothing
Set objqsend = nothing
Set objmessage = nothing
%>

We have provided an example of the basic knowledge of MSMQ. The following describes how to use it!
MSMQ can be used in many applications, but the most common application is to uninstall the process in another thread. (For example, IIS on the same machine as MSMQ) or IIS on another machine. By uninstalling these blocked processes, ASP programs can continue to run.

In general, determine whether to uninstall the task process to do two things:

The first is based on the running time of the process.

Second, it depends on whether the user has responded (for example, a user in the chat room has not spoken for several hours ).

For example, if a Web page task on the server takes too long, the user will get an error message indicating that the webpage times out, you can click Refresh again or simply discard the webpage. But now we can change the processing method, such as background processing, rather than simply adding the processing time. You must know that the background processing method can also improve the website performance.

MSMQ also has the ability to control a specific COM object in the message body. This object only supports the idispatch and ipersist (ipersiststream or ipersiststorage) interfaces.

The most common ones are ADODB. recordset (or Ador. recordset) and word. Document. Here is an example of processing ADODB. recordset.

For example, how to deal with ADODB. recordset:
Public sub sendrecordsetinmessage ()
Dim objqinfo as new MSMQ. msmqqueueinfo
Dim objqsend as MSMQ. msmqqueue
Dim objmessage as new MSMQ. msmqmessage
Dim objrs as new Ador. recordset
Dim A as new msmqqueue
With objrs
. Cursorlocation = aduseclient
. Fields. APPEND "FN", advarchar, 25
. Fields. APPEND "ln", advarchar, 25
. Open
. Addnew
. Fields ("FN") = "Chris"
. Fields ("ln") = "blexrud"
. Update
. Addnew
. Fields ("FN") = "Shayna"
. Fields ("ln") = "blexrud"
. Update
End
Objqinfo. pathname = "./test"
Set objqsend = objqinfo. Open (mq_send_access, mq_deny_none)
Objmessage. Label = "recordset state !!!! "
Objmessage. Body = objrs
Objmessage. Send objqsend
Objqsend. Close
Set objqinfo = nothing
Set objqsend = nothing
Set objmessage = nothing
Set objrs = nothing
End sub

Have you understood MSMQ? I think you have a good solution to the ASP process deadlock! I hope you can learn more about MSMQ and how to control ASP processes through MSMQ!

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.