Message Processing in Asp. Net --- MSMQ series Learning (1), asp.net --- msmq

Source: Internet
Author: User
Tags msmq

Message Processing in Asp. Net --- MSMQ series Learning (1), asp.net --- msmq

Just one year after graduation, I was impetuous. I was asked about Message Queue during my last interview. I felt very ashamed because I didn't know it, so I decided to use it. I have heard of such a thing before, but I have no idea what to say and how to use it in any scenarios, because I have never used it in projects, have no confidence in talking to the interviewer. Now, let's start learning step by step!

First, we need to know what a message queue is? I am confused by network explanations. Let's explain it in a simple way. As the name suggests, a message queue must first be a queue, what about queue operations? The main class is queuing and queuing, that is, you have a program that generates the message content (that is, the content you want to send, such as a string, A book, etc.), that is, entering the queue (that is, the producer). Another program reads the message queue and the content of the message you send to the queue. This is the departure (consumer ); in this case, when will we use the message queue? To sum up, when you do not need to get results immediately, but the concurrency cannot be infinitely large, it is almost the time when you need to use message queues. for example, when you write logs, a client has multiple operation zones to write logs, and many clients to write logs. Obviously, concurrency cannot be infinite, therefore, you need to put the log writing request into the message queue, and take out the queue messages from the consumer side in sequence and write them into the log.

 

1. Install MSMQ

About MSMQ introduction I will not say much, want to know their own Baidu, http://www.oschina.net/translate/top-10-uses-for-message-queue

The specific installation process is the control panel-> uninstall the program-> enable or disable the Windows function, select MSMQ, for example, click OK;

 

The method for creating a queue is as follows. netFramework China provides the static method Crate () in the MessageQueue class to create a message queue. Its definition is as follows:

1 1 // 2 3 2 // Summary: 4 5 3 // create a non-transactional "message queue" queue in the specified path. 6 7 4 // 8 9 5 // parameter: 10 11 6 // path: 12 13 7 // path of the queue to be created. 14 15 8 // 16 17 9 // return result: 18 19 10 // indicates the System. Messaging. MessageQueue of the new queue. 20 21 11 public static MessageQueue Create (string path); 22 23 12 // 24 25 13 // Abstract: 26 27 14 // create a transactional or non-transactional "message queue" queue in the specified path. 28 29 15 // 30 31 16 // parameter: 32 33 17 // transactional: 34 35 18 // true if a transaction queue is created; if a non-transaction queue is created, it is false. 36 37 19 // 38 39 20 // path: 40 41 21 // path of the queue to be created. 42 43 22 // 44 45 23 // return result: 46 47 24 // indicates the System. Messaging. MessageQueue of the new queue. 48 49 25 public static MessageQueue Create (string path, bool transactional );View Code

 

Ii. Create, delete, and manage queues

To compile a Message Queue program on the. Net platform, we must learn a very important class MessageQueue, which is located in the namespace System. Messageing. There are several common methods that must be mastered:

--- Create method: Create a New Message Queue using the specified path

--- Delete method: Delete an existing Message Queue

--- Existe method: Check whether the specified message queue exists

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

--- Peek/BeginPeek method: view the message queue in a specific queue, but do not remove the message from the queue

--- Receive/BeginReceive method: retrieve the most advanced message in the specified message queue and remove it from the queue.

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

--- Purge method: clears messages in a specified queue.

 

3. Send and serialize messages

The message defined in MSMQ message queue is composed of a subject and several attributes. The message subject can be composed of text and binary, and can be encrypted as needed, the Message Size in MSMQ cannot exceed 4 MB.

1. Message sending steps: connect to the queue-> specify the Message format-> provide the data to be sent (subject)-> call the send () method to send the message;

2. serialize messages: Message serialization can be completed through three predefined formatting programs attached to the. Net Framework,

XMLMessageFormatter, BinaryMessageFormatter, ActiveXMessageFormatter

3. Message sending instance

There is a need to send a book to the queue through the message queue, and then read it from the message queue. The basic information of a book includes its number, name, author, and pricing. How can this complex object type be transmitted? The details are as follows:

Transfer object

Using System; using System. collections. generic; using System. text; namespace MSMQ {public class Book {public int BookId {get; set;} public string BookName {get; set;} public string BookAuthor {get; set ;} public double BookPrice {get; set ;}}}Book

 

MSMQ operations

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. messaging; using System. windows. forms; namespace MSMQ {class MSMQOperation {/// <summary> /// Create a new message queue using the specified path using the Create method /// </summary> /// <param name = "queuePath"> </param> public static void Createqueue (string queuePath) {try {if (! MessageQueue. Exists (queuePath) {MessageQueue. Create (@ ". \ private $ \ myQueue"); MessageBox. Show ("the queue is created successfully! ");} Else {MessageBox. Show (queuePath +" already exists! ") ;}} Catch (MessageQueueException e) {MessageBox. show (e. message) ;}//< summary> // connect to the Message queue and send the Message to the queue /// </summary> public static bool SendMessage (Book book) {bool flag = false; try {// connect to the local queue MessageQueue myQueue = new MessageQueue (". \ private $ \ myQueue "); System. messaging. message myMessage = new System. messaging. message (); myMessage. body = book; myMessage. formatter = new XmlMessageFormatter (new Type [] {typeof (Book)}); // send the message to myQueue in the queue. send (myMessage); flag = true;} catch (ArgumentException e) {MessageBox. show (e. message);} return flag;} public static string ReceiveMessage () {MessageQueue myqueue = new MessageQueue (@". \ private $ \ myQueue "); myqueue. formatter = new XmlMessageFormatter (new Type [] {typeof (Book)}); try {System. messaging. message myMessage = myqueue. receive (); Book = myMessage. body as Book; return string. format ("No.: {0}, Title: {1}, Author: {2}, pricing: {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

The simple usage here is introduced here. Later I want to learn petshop and learn the order processing strategy, which is said to be good.

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.