Use C # And MSMQ to develop a message processing program

Source: Internet
Author: User
Tags msmq

Http://www.cnblogs.com/Ansel/archive/2005/11/28/286073.html

Introduction

 

MSMQ (Microsoft Message Queue) is the basis of message applications in Windows operating systems and a development tool for creating Distributed and loosely connected messaging applications. Message queues and emails have many similarities. They all contain multiple attributes for saving messages. The addresses of senders and receivers are indicated in message types; however, their usefulness is very different: the sender and receiver of a message queue are applications, while the sender and receiver of an email are usually people.

 

Like an email, the sender and receiver of A Message Queue do not need to be present at the same time. They can be stored in a message queue or an email server. Therefore, we can describe the architecture of the MSMQ application:

It can be seen that developing MSMQ applications is not very difficult. However, to use MSMQ to develop your message processing program, you must install message queue on the development system and host that uses the program. The installation of message queue is the installation of Windows components, which is similar to that of general components. After the MQ is installed, you can develop your own message processing program. However, if your computer is in a working group rather than a domain, your public queue may not be available, but this does not affect your program development.

 

The message processing program sends and receives messages. However, to send and receive messages, you must also reference a queue. Generally, we reference public queues and dedicated queues, both of which store user-generated messages. After the queue is referenced, you can send, receive, and read messages. The message receiving service is located in system. messaging. If you cannot find this namespace, you must manually add it. Click [add reference] in [project], click the Browse button, find the system. messaging. dll file, and add it.

 

Reference queue

 

There are three methods to reference a queue: using a path, a format name, and a tag to reference a queue. Here I will only introduce the simplest and most commonly used method: using a path to apply a queue. The queue path is in the form of machinenamequeuename. The path to the queue is always unique. The following table lists the paths used for each type of queue:

Queue type

Syntax used in the path

Public queue

Machinenamequeuename

Dedicated queue

Machinenameprivate $ queuename

Log queue

Machinenamequeuenamejournal $

If it is sent to the local machine, you can also use "." To represent the local name. The specific reference method is carried out through the path attribute, or when the message queue is initialized.

 

If a message queue is referenced during initialization, the message queue must exist in the system; otherwise, an interruption occurs. It is very easy to add a queue to the system. Open [Computer Management] in [control panel], expand [services and applications], find and expand [Message Queue] (if not found, indicates that you have not installed a message queue.) Right-click the type of the Message Queue you want to add and select create queue. Of course, you can also create a message queue in the program. The following describes how to create a message queue. The code for referencing a Message Queue during initialization is very simple, as shown below:

 

Messagequeue MQ = new messagequeue (". \ private $ \ Jiang ");

The code for referencing a Message Queue through the path attribute is also very simple:

Messagequeue MQ = new messagequeue ();

MQ. Path = ". \ private $ \ Jiang ";

UseCreateYou can create a queue on a computer:

System. messaging. messagequeue. Create (@ ". Private $ Jiang ");

 

Send message

 

After the queue is referenced, the message can be sent. Messages can be sent into simple messages and complex messages. simple messages are commonly used data types, such as integer and string data; the Data Types of complex messages usually correspond to complex data types in the system, such as structures and objects.

 

An example of sending a simple message is as follows:

MQ. Send (1000); // send Integer Data

MQ. Send ("This is a Test message !"); // Send a string

 

The transmission of complex messages is similar to that of simple messages. Only when a message is sent, the variable is used to send the message content instead of directly sending the message content. The following code sends a complex message through the message variable and complex data type variable respectively.

 

// The message sent in the following code is represented by the message variable

Message MSG;

MSG = new message ("a complex message !");

MSG. Label = "this is the label ";

MSG. Priority = messagepriority. High;

MQ. Send (MSG );

 

// The message sent in the following code is represented by a variable of the complex data type. Customer is a custom class.

          Customer customer = new Customer();

Customer. lastname = "Copernicus ";

Customer. firstname = "niclaus ";

MQ. Send (customer );

 

Receive messages

 

Receiving messages is a little more complex than sending messages. Two methods are used to receive a message: the receive method is used to receive the message and permanently delete the message from the queue. The peek method is used to retrieve the message from the queue without removing the message from the queue. If you know the message identifier (ID), you can use the receivebyid method and the peekbyid method to complete the corresponding operation.

 

The message receiving code is simple:

MQ. Receive (); // or MQ. receivebyid (ID );

MQ. Peek (); // or MQ. peekbyid (ID );

 

Read messages

 

Only the messages received can be read out. Therefore, you must read the messages after receiving them. Reading the messages is the most complex operation. The messages that can be read by the application are in different formats from those in the message queue. Therefore, the messages sent by the application are serialized before being sent to the Message Queue. This process is automatically completed by the system, program developers do not have to write code for this, but they are faced with message serialization after receiving messages.

 

Message serialization can be performed through Visual Studio and. net Framework comes with three predefined formatters to complete: xmlmessageformatter object (default formatter setting of the messagequeue component), binarymessageformatter object, and activexmessageformatter object. Because messages formatted by the latter two cannot be read by humans, we often use xmlmessageformatter objects.

 

The following code uses the xmlmessageformatter object to format a message:

          string[] types = { "System.String" };

(Xmlmessageformatter) MQ. formatter). targettypenames = types;

              Message m=mq.Receive(new TimeSpan(0,0,3));
After the received message is sent to the Message variable, the message can be read through the body attribute of the message variable M:
       MessageBox.Show((string)m.Body);
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.