Use C # And MSMQ to develop a message processing program

Source: Internet
Author: User
Tags msmq

Introduction

 

MSMQ(Microsoft Message Queue) isWindowsMessage application in the operating systemProgramIs used to create distributed, 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 describeMSMQApplication Architecture:

It can be seen that the developmentMSMQApplications are not very difficult. But useMSMQTo develop your message processing program, you must install message queue on the development system and the host using the program. Message Queue InstallationWindowsThe installation of components 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 inSystem. messagingIf you cannot find the namespace, you must manually add it. Click[Project]In[Add reference], Click Browse to findSystem. messaging. dllFile.

 

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 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 $

You can also use"."Indicates the name of the local machine. The specific reference method isPathYou can also initialize the message queue.

 

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.[Control Panel]In[Computer Management], Expand[Services and Applications], Locate and expand[Message Queue](If no message queue is found, you have not installed it.) 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. TheCodeIt is very simple, as shown below:

 

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

PassPathThe code for referencing a Message Queue with attributes is also very simple:

Messagequeue MQ = new messagequeue ();

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

Use Create You 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:

Message Queue. Send (1000 );//Send Integer Data

MQ. Send ("This is a Test message !"); //Sending 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 messages sent in the following code are represented by message variables.

Message MSG;

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

MSG. Label = "this is the label ";

MSG. Priority = messagepriority. High;

MQ. Send (MSG );

 

//The messages sent in the following code are represented by complex data type variables., CustomerFor a custom class

 
Customer customer =NewCustomer ();

Customer. lastname = "Copernicus ";

Customer. firstname = "niclaus ";

MQ. Send (customer );

 

Receive messages

 

Receiving messages is a little more complex than sending messages. The following two methods are used to receive messages:ReceiveTo permanently delete messages from the queue.PeekMethod to retrieve messages from a queueInstead of removing the message from the queue. If you know the message identifier (ID), You can also useReceivebyidMethod andPeekbyidMethod to complete the corresponding operation.

 

The message receiving code is simple:

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

MQ. Peek ();//OrMQ. 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 achieved throughVisual StudioAnd. NET FrameworkThe following three predefined formatting programs are included:Xmlmessageformatter Object ( Messagequeue Default Formatting settings of components ),Binarymessageformatter Object,Activexmessageformatter Object. Because the messages formatted by the latter two cannot be read, we often useXmlmessageformatterObject.

 

UseXmlmessageformatterThe code for formatting an object message is as follows:

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 variableMOfBodyThe message can be read by attributes:
 
MessageBox. Show (string) M. Body );

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.