Asp. NET Advanced Applications (3)

Source: Internet
Author: User
Tags define message queue numeric value msmq
Asp.net| advanced use of MSMQ

MSMQ (Microsoft News queue, Microsoft Message Queuing) is an asynchronous mode of communication between multiple different applications that can be distributed across the same machine or anywhere in a connected network space with each other. The idea is that the sender of the message puts the information he wants to send into a container (which we call the message), and then saves it to a system common-space messages queue , a local or offsite message receiver is then removed from the queue to process the message sent to it.

In the message passing mechanism, there are two more important concepts. One is a message, the other is a queue. Messages are information that is required by both sides of the communication, and it can be a variety of media, such as text, sounds, images, and so on. The final understanding of the message, for the two sides of the message to agree beforehand, the advantage is that the first is equivalent to a simple encryption of data, the second is the use of their own definition of the format can save the transfer of traffic. A message can contain the identity of the sending and the recipient, so that only the specified user can see the information that is passed to him and return a successful receipt for the operation. Messages can also contain timestamps to facilitate processing of certain time-related applications by the receiver. The message can also contain the expiration time, which indicates that if the message has not arrived within the specified time, it will be invalidated, and this application will be more closely related to the time association.

Message Queuing is a public storage space for sending and receiving messages that can exist in memory or in physical files. Messages can be sent in two ways, express and recoverable (recoverable), the difference is that the express way for the rapid delivery of messages, the message is placed in memory, rather than on the physical disk to obtain higher processing power The recoverable mode writes messages to the physical disk in each step of the transfer process to obtain better recovery capability. Message queues can be placed on the machine where the sender, receiver is located, or on a separate machine. It is precisely because of the flexibility of the message queue in the placement mode that the reliability of the message transmission mechanism is formed. When a machine that holds Message Queuing fails and restarts, messages sent in recoverable mode can be restored to the state before the failure, and messages sent by courier are lost. On the other hand, using the messaging mechanism, the sender needs to worry about whether the receiver is starting, failure, and so on non-essential factors, as long as the message successfully sent out, you can think that the processing is completed, and actually the other side may not even boot, or actually completed the transaction may be the next day.

The benefits of using MSMQ are: because it is asynchronous communication, both the sender and the receiver do not have to wait for the other to return the success message, they can execute the remaining code, thus greatly improving the ability to deal with things; in the process of information transmission, the information sending mechanism has a certain function of failure recovery capability MSMQ's messaging mechanism makes it possible for both sides of the message communication to have a different physical platform.

The MSMQ features provided on Microsoft's. NET platform make it easy to create or delete message queues, send or receive messages, and even manage message queues.

In. NET products, an MSMQ class library "System.Messaging.dll" is provided. It provides two classes that operate on message objects and Message Queuing objects, respectively. Before you can use MSMQ functionality, you must make sure that the MSMQ Message Queuing component is installed on your machine and that the service is running. When using ASP.net programming, you should use it in your head:

<%@ Assembly name= "System.Messaging"%>

<%@ Import namespace= "system.messsaging"%>

Introducing the MSMQ class library into the ASP.net file

1. Creating a message Queue

Dim msgque As MessageQueue

Msgque=new MessageQueue (Msgpath)

Where: Msgpath can be a local private queue, such as ". \MyQueue", or it can be a public queue for other machines, such as "Saidy\777$\myqueue", Saidy for another machine name.


2. Sending messages

Dim msgque As MessageQueue

Msgque.send (MSG)

Wherein: MSG is any object.

3. Receipt of messages

The reception of messages is divided into synchronous and asynchronous ways, synchronously receives the first message received from the message queue within a specified time, the program is in a waiting state when there is no message in the message queue, and the asynchronous receive method defines an event handler that triggers the function as soon as the first message arrives in the message queue.

1) Synchronous mode

Dim Msg As Message

Dim Fmt As XMLMessageFormatter

fmt= CType (Msgque.formatter,xmlmessageformatter)

Fmt.targettypenames = new String () {"System.String"}

Msg=msgque.receive (New TimeSpan (0,0,3))

First define the format in which the message should be converted, and then receive the message within a specified time

2) Asynchronous mode



Dim Fmt As XMLMessageFormatter
' Define Receive Message type
FMT = CType (msgque.formatter,xmlmessageformatter)
Fmt.targettypenames = new String () {"System.String"}

' Define the message handler function entry
AddHandler msgque.receivecompleted, New Receivecompletedeventhandler
(AddressOf onreceivecompleted)

' Define the message handler function
Public Shared Sub onreceivecompleted (s as Object, asyncresult as Receiveasynceventargs)
Dim Msgque as MessageQueue = CType (s,messagequeue)
Dim MSG as Message = Msgque.endreceive (Asyncresult.asyncresult)
' At this time Msg.body is the message object that is being fetched
Msgque.beginreceive ()
' Redefining asynchronous Receive mode
End Sub

' Start asynchronous Receive mode
Msgque.beginreceive


Message Queuing Configuration Properties

About the properties of queues

Path attribute: It can determine three ways to reference queues, path references, format name references, identity references

Category property: Identifies the type of queue currently in use. Category is the GUID value defined by the queue owner. The GUID value can be generated by a GUID generation tool or a user-defined numeric value. The GUID value is not unique so that multiple message queues can be divided into different categories (category) based on the same GUID value.

Properties associated with the sending data type

Formatter property: Determines the order in which messages are sent and received in a queue, and what content can be sent in a message.

Properties related to queue interaction

Denysharereceive property: Determines that only one part of the same time can access messages in Message Queuing.

CanRead and CanWrite properties: Determines whether a queue can be read or written.

Maximumqueuesize and MaximumJournalSize Properties: Sets the maximum message capacity for a queue (journal queue) in kilobytes. Once the received message reaches this capacity, the new message will no longer be received.

In general, the maximum value for Message Queuing is set by the Message Queuing administrator, and if this value is not controlled, the default maximum capacity of Message Queuing will be unlimited.

UseJournalQueue Property:: Sets whether to copy incoming messages to the log message queue.

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.