Detailed. NET Message Queuing (MSMQ) app

Source: Internet
Author: User
Tags message queue msmq

"IT168 Technical Documentation" MSMQ is a component of Windows 2000, Windows XP, Windows Server 2003, and will continue to be included in Windows Vista and later Windows servers. Even if the target receiving application is not running, or the computer running the sending or receiving application is not networked, the cooperative application can still use MSMQ to send and receive messages between each other. Messages are stored and forwarded by MSMQ before they reach the destination queue. The receiving application is able to recover data from the queue.

The main feature of Message Queuing, such as MSMQ, is that it separates the sending and receiving applications so that they do not have to run concurrently. This means that an application can store data in a queue, regardless of whether items in the queue are routed to the receiving application.

MSMQ is an optional component of Windows that you can install by simply adding or removing the Windows Components Wizard from the Windows Control Panel. MSMQ has two configuration modes: domain mode or Workgroup mode (only private queues are used). Once the MSMQ installation is complete, you can do so immediately. NET application to run the.

  Why use Message Queuing

You might think that you can apply Message Queuing through a simple database table where one application writes data and another application reads data from it. Message Queuing platforms are more stable because they often have their own security mechanisms, transactional support, and other functions. The routing feature for transmitting messages is one of its key applications. MSMQ provides a variety of Message Queuing platforms.

MSMQ Interaction

Developing message-based applications starts from the queue. MSMQ contains four types of queues:

Outgoing queue: Use it to temporarily store messages before they are sent to the destination.

Public queues: advertised in the Active Directory. Applications on various servers throughout the network can find and apply public queues through the Active Directory.

Private queues: These are queues on the local server that are not valid for other servers (so these queues are not advertised in the Active Directory.) )

System queues: Contains journal queues (System generated), dead queues, and transactional dead-letter queues. Dead messages cannot be delivered.

The System.Messaging namespace performs the programmatic operation of MSMQ. There are two main objects in this namespace:

Message: The actual message or data that the queue sends or reads.

MessageQueue: The MSMQ message queue that receives/sends messages.

  MSMQ Programming (all the code is in the demo)

Establishing a queue is the first step in applying MSMQ. You can do this either through the Message Queuing option in the Windows Computer Management console or by programming a queue yourself. The C # code in List a establishes a new private MSMQ message queue (if no queue exists) and creates a message at the same time.

The code applies the Exists method of the MessageQueue class to determine whether there is a private queue named TechRepublic. If present, it uses an existing queue example of this MessageQueue object; otherwise, a new queue is established.

The new Message object is used to send a message to the queue. Its Label property specifies the message title displayed in the MSMQ console, whose body contains the contents of the item that is placed on the queue. In this case, I only send text, but you can use any type of object. The Send method of the MessageQueue class sends a message to the queue. The corresponding vb.net code is in list B.

The next step is to read the message from the queue. This is a simple process that applies the Receive method of the MessageQueue class. If a message exists in the queue, the Receive method returns a Message object, otherwise it waits for a message to appear (you can set a time period). Recovering an object from a queue requires a prior knowledge of its type.

The formatter property of the MessageQueue class allows you to easily specify the type of object being recovered. The following simple example uses text only, so it applies system.string. In List C, C # code reads a message from the test queue.

The TimeSpan object that is submitted to the Receive method specifies the system's wait time when the exception occurs. Next, set the formatter method in this example, and the object is converted to a string to read the previously stored text. The Receive method reads a message from the queue and its value is displayed in the console. In the last part of the try block, the queue is closed.

 Easily apply messages

MSMQ combines windows and. NET System.Messaging namespaces, making it easy for you to leverage messages in. NET Applications. Messages provide a powerful tool for sending and receiving messages (data) asynchronously in an enterprise application.

1. Send a simple message

Before the message is sent, the first thing to do is to add the components of the message queue in the Control Panel--Add the removal program--Add the component of the messages queuing, then establish a private message queue name, and finally, instantiate an instance of MessageQueue in the using and specify its corresponding queue. The following code

Instantiate the MessageQueue, and point to an existing name called Videoqueue queue
MessageQueue MQ = new MessageQueue (@ ". \private$\videoqueue");
Mq. Send ("Message Test", "test Message");

2. Send Complex message: Demo (note: When running, first establish a private queue named ". \private$\article")

Detailed. NET Message Queuing (MSMQ) app

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.