Message Processing---MSMQ in ASP.

Source: Internet
Author: User
Tags msmq

Just graduated a year, more impetuous, the last interview was asked the message queue, feel very ashamed because do not know, so determined must learn to use it. Previously just heard that there is such a thing, to say what, in what scenario use is not known, because they do not have in the project, there is no confidence to go to the face of the trial officials to talk about. Well, start learning now!

First we need to know what the message queue is? I was the explanation of the network on the flicker of a Leng Leng, to a personal popular explanation, the message queue as the name implies it first has to be a queue, the operation of the queue is the main team and the squad, that is, you have a program after the message content (that is, you want to send content, such as a string, a book, etc. That is, the queue (that is, the producer), another program read Message Queuing, read the message you send into the content, this is the team (consumer); So what is the situation in general we will use Message Queuing? Sum up the word of the great God: when you don't need immediate results, but the amount of concurrency is not infinitely large, It's pretty much the time you need to use Message Queuing. For example, when you write a log, a client has more than one operation area to write logs, and there are many clients to write, obviously concurrency can not be infinite, so you need to write log requests into the message queue, the consumer side to take out the queue of messages written to the log.

I. Installation of MSMQ

About MSMQ Introduction I don't say much, want to know Baidu, Http://www.oschina.net/translate/top-10-uses-for-message-queue

The specific installation process is the Control Panel, uninstall program, open or close Windows features, select MSMQ, such as, click OK;

There is another way to create a queue, which is implemented by code programming, in. NETFramework China provides a static method crate () under the MessageQueue class to create a message queue, which is defined as follows

1 1//2 3  2//Summary:4 5  3//creates a non-transactional "Message Queuing" queue in the specified path. 6 7  4//8 9  5//Parameters:Ten  One  6//Path: A  -  7//The path of the queue to be created.  -  the  8// -  -  9//return Result: -  + Ten//represents the System.Messaging.MessageQueue of the new queue.  -  +11 Public StaticMessageQueue Create (stringpath); A  at  A// -  -  -//Summary: -  -  -//creates a transactional or non-transactional "Message Queuing" queue in the specified path.  -  in  the// -  to  -//Parameters: +  -  -//Transactional: the  *  -//true if a transactional queue is created, or false if a non-transactional queue is created.  $ Panax Notoginseng  +// -  the  -//Path: +  A  +//The path of the queue to be created.  the  +  A// -  $  at//return Result: $  -  -//represents the System.Messaging.MessageQueue of the new queue.  -  the25 Public StaticMessageQueue Create (stringPathBOOLtransactional);
View Code

Two. Create, delete, and manage queues

In the program of writing Message Queuing under the. NET platform, we must learn a very important class MessageQueue, which is located under Namespace System.messageing, which has several common methods that must be mastered:

---Create method: creates a new message queue using the specified path

--- Delete method: Delete the existing message queue

--- Existe method: To see if a specified message queue exists

--- getallmessages () method: Get all messages in the queue

--- Peek/beginpeek method: View Message Queuing in a particular queue, but do not remove messages from this queue

--- Receive/beginreceive Method: Retrieves the first message in the specified message queue and removes it from the queue

--- Send method: Sends a message to the specified message queue

--- Purge method: Clears the message for the specified queue

Three. Sending and serializing messages

Messages defined in the MSMQ message queue consist of a principal and several attributes, the body of the message can be text, binary, and, optionally, encrypted, and the message size in MSMQ cannot exceed 4M.

1. Send Message step: Connection queue, specify message format, provide the data to be sent (subject), call the Send () method to send the message out;

2. Serialization of messages: Message serialization can be passed. NET Framework to complete the three predefined formatters that are included with the

Xmlmessageformatter,binarymessageformatter,activexmessageformatter

3. Sending an instance of a message

Now there is a requirement to send a book message to the queue via Message Queuing and then read it from the message queue. The basic information of the book includes the book number, book name, book author and book pricing, such a complex object type how to transfer it? Details are as follows:

Passing objects

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacemsmq{ Public classBook { Public intBookId {Get; Set; }         Public stringBookName {Get; Set; }         Public stringBookauthor {Get; Set; }         Public DoubleBookprice {Get; Set; }    }}
Book

MSMQ operation

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingsystem.messaging;usingSystem.Windows.Forms;namespacemsmq{classmsmqoperation {/// <summary>        ///Create a new message queue with the specified path by creating method/// </summary>        /// <param name= "QueuePath" ></param>         Public Static voidCreatequeue (stringQueuePath) {            Try            {                if(!messagequeue.exists (QueuePath)) {Messagequeue.create (@". \private$\myqueue"); MessageBox.Show ("Create queue successfully! "); }                   Else{MessageBox.Show (QueuePath+"already exist! "); }                                }            Catch(messagequeueexception e) {MessageBox.Show (e.message); }        }         /// <summary>        ///connecting message queues and sending messages to queues/// </summary>         Public Static BOOLSendMessage (book book) {BOOLFlag =false; Try            {                //Connect to a local queueMessageQueue myqueue =NewMessageQueue (". \\private$\\myQueue"); System.Messaging.Message Mymessage=NewSystem.Messaging.Message (); Mymessage.body=Book ; Mymessage.formatter=NewXMLMessageFormatter (NewType[] {typeof(book)}); //send a message to the queuemyqueue.send (mymessage); Flag=true; }            Catch(ArgumentException e) {MessageBox.Show (e.message); }            returnFlag; }         Public Static stringReceiveMessage () {MessageQueue myqueue=NewMessageQueue (@". \private$\myqueue"); Myqueue. Formatter=NewXMLMessageFormatter (NewType[] {typeof(book)}); Try{System.Messaging.Message Mymessage=Myqueue.              Receive (); Book Book= Mymessage.body asBook ; return string. Format ("number: {0}, Title: {1},{2}, Price: {3}", book. BookId, book. BookName, book. Bookauthor, book.            Bookprice); }            Catch(messagequeueexception e) {MessageBox.Show (e.message); }            Catch(InvalidCastException e) {MessageBox.Show (e.message); }            return NULL; }    }}
msmqoperation

Here is the simple use of the introduction here, later want to learn petshop, study the next single processing strategy is said to be good

Message Processing---MSMQ in ASP.

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.