Understanding MSMQ, controlling the ASP process

Source: Internet
Author: User
Tags goto iis message queue msmq

We often encounter these situations when using ASP programs: A process takes too long to cause the system to appear "Server is too busy" when the client has expired, the visitor has abandoned access to your site, or a large number of dead queues have been blocked from your server. Error message.

When you're in the process of designing a Web site, an effective solution is to use Microsoft message Queue (MSMQ) to end these processes and get the site back on track!

What kind of thing is MSMQ? Let's take a look at the following:

A basic introduction to Microsoft message Queue:

MSMQ (code-named "Falcon") is a service running in Windows NT that provides asynchronous communication between applications. You can find it in the NT4 Option Pack. The basic concept of MSMQ is simple: it can be viewed as an email between applications: A message is packaged into a particular type of container, and the message is saved in a specially-functioning queue until the recipient accepts the message. These queues ensure the delivery of MSMQ regardless of the current network connection status.

Like all e-mail messages, an MSMQ message has a sender and a recipient, and the receiver should be able to access the queue. A separate message in a single queue that has multiple recipients such as Respinder. The sender of the message is typically Web Server (IIS).

MSMQ can also communicate with other messaging systems. For example: Sun Solaris, HP-UNIX,OS/2, VMS, as/400 platform. Like other BackOffice services, MSMQ has a COM API (Mqoa.dll) for developers to develop programs. The three most commonly used classes are: MSMQQueueInfo, MSMQQueue, MSMQMessage.

(1), Msqmqueueinfo

MSMQQueueInfo allows you to create new, open, and delete messages in the queue. To establish contact with a queue, you first need to set up PathName, which is the property of a named queue that tells msqm which machine is 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 code above opens a local queue called Myqueue. If the queue is on another server, the code should be like this:

Objqueue.pathname = "Someothercomputer\myqu"

There are two parameters in the open queue: Access and ShareMode. Access indicates what action will be performed on the queue. There are generally three operations:

Mq_peek_access, Mq_receive_access (1), mq_send_access (2).

Mq_peek_access is used to find messages in a particular queue. However, no action is performed on the message.

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 queues, but not to receive messages.

Note that a MSMQQueue object is returned after using the open operation. The following is a typical example of a new and deleted operation:
<%
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 that is opened in an MSMQ service. This class provides a function for looping messages in the pointer queue. You can't open a queue that uses the MSMQQueue class. You can only use Msqmqueueinfo (see the example above), although many ASP applications often use MSMQ to send messages, but most of the time you need an ASP to display the details of the message.

There are two ways to get a message: synchronous, asynchronous, but the ASP can only use synchronous methods. This is because the ASP is not able to declare a WithEvents variable on the server side.

Here is an example of using MSMQ in an asynchronous way (VB only)
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:" & 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 establishes a queue (if it does not yet exist). The M_objmsmqevent object is then connected to the MSMQQueue object by calling EnableNotification. Once connected to the MSMQEvent object, the next thing you need to do is to complete the arrived and Arrived_error (optional) events. Arrived event will be triggered when a new message arrives at the queue the event returns two pointers, one pointing to where the message should never be read in the queue, and the other is the current location. If an error occurs, the Arrivederror event is triggered when the message is synchronized, the program is not suspended until the message is available or timeout. 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 no 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 the properties of a message in a queue. MSMQ messages have two methods and a wide range of properties. Two of the most important properties are: Body and LabeL. The most important method is Send. There are two ways to get the message: opening, peeking. When the opening method is used, the message is deleted, and when the peeking method is used, the message is still kept in the queue until it expires. Their return value is a 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 are 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 looks for messages in the queue, using the Receive method, which basically sets a timeout of 1000 microseconds, and it tells MSMQ1000 microseconds to stop the lookup setting a very segment timeout function is primarily used to check for the presence of messages rather than waiting for a message. That is to say, if you want to see if you have any messages you can use this method. If there is no message, the returned pointer is empty (if not objmessage is nothing). Here is the code to send 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 are the body."
Objmessage.send Objqsend
Objqsend.close
Set Objqinfo = Nothing
Set objqsend = Nothing
Set objmessage = Nothing
%>

About the basic knowledge of MSMQ we have done an example, the following will be its specific application of understanding and discussion!

MSMQ can be used in many applications, but the most common use is to uninstall processes from another thread. (such as IIS on the same machine as MSMQ) or IIS on a different machine. By uninstalling these blocked processes, you can enable the ASP program to continue to run.

In general, there are two things you should do to determine whether you need to uninstall a task process:

One is based on the time that the process runs.

The second is based on whether the user has a response (for example, a user in a chat room has not spoken for hours).

For example, if a server-side Web page task takes too long, the user gets a page timeout error message, we can generally refresh or simply discard the page by clicking it again. But now you can change the way you handle things, such as background processing, rather than simply adding time to the page. You know, background processing can also improve the performance of the site.

MSMQ also has the ability to control a specific COM object in the body of a message. This is OK as long as the object supports the Idispatch and IPersist (IPersistStream or IPersistStorage) interfaces.

One of the most commonly used, can support the two is ADODB. Recordset (or Ador. Recordset) and Word.Document. In the following we give a processing ADODB. Recordset example.

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 With
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

Do you know about MSMQ? I think now for the process of ASP deadlock, you already have a very good solution for it! I hope you can learn more about MSMQ and how to control the ASP process through MSMQ.



Related Article

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.