Understanding MSMQ, controlling the ASP process (i) (paste)

Source: Internet
Author: User
Tags goto message queue msmq
Process | Control We often encounter these situations when using ASP programs: A process takes too long to cause a client to expire, the visitor has abandoned access to your site, or a large number of dead queues are blocked on your server, and the system appears "server is too Busy "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.



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.