(Switch) Continue MSMQ

Source: Internet
Author: User
Tags msmq

Original Author: devout people click here to send to original

In the previous article, I briefly introduced the related concepts of MSMQ.

Message

Message is the data storage unit of MSMQ, and our user data is usually filled in the message body. Therefore, it is very important. Let's take a look at its embodiment in. net,

As shown in the figure, message provides three constructors. The parameter body represents our user data. When we pass in the constructor, the data will be assigned to the body with the same name. The formatter parameter corresponds to the formatter with the same name. It is a serializer. When our user data is of a complex type, for example, the message will automatically use this serializer to serialize our complex types. Message supports three types of serialized objects:

-- Xmlmessageformatter object ---- default formatter settings of the messagequeue component.

-- Binarymessageformatter object;

-- Activexmessageformatter object;

Because messages formatted by the latter two cannot be read by humans, we often use xmlmessageformatter objects. The object constructor has three reloads:

public XmlMessageFormatter(); public XmlMessageFormatter(string[] targetTypeNames); public XmlMessageFormatter(Type[] targetTypes);
MSMQ queue

Messages must be stored in MSMQ queues. system. messaging. messagequeue is used in. Net to manage MSMQ queues. It provides the vast majority of APIs that can operate MSMQ, such

1. Determine whether the queue in the specified path exists. Path indicates the queue path, in the form of "Host Name \ queue name", for example :". \ private $ \ myqueue ", where ". "indicates the local host," \ private $ \ myqueue "indicates the queue name, and" Private $ "indicates that we created a dedicated queue, on the network, we can use the path to uniquely identify a queue.

public static bool Exists(string path);

2. Create a queue. Path indicates the queue path, and transactional indicates whether to create a transaction queue. The default value is fasle. I have discussed the transaction queue in detail in the previous article, which is not repeated here.

public static MessageQueue Create(string path);public static MessageQueue Create(string path, bool transactional);

3. delete a queue

public static void Delete(string path);

4. Send a message to MSMQ. OBJ indicates our user data, and transation indicates that our sending operations are included in the transaction. As we mentioned earlier, MSMQ receives messages, but here we can see that the send operation does not require us to use the Message Type parameter. This is because when I pass in an object parameter data, a message object is automatically created for us within the send operation, in addition, the passed object parameters are serialized using the default serializer, and then loaded into the body attribute of the message. If we specify the label attribute in the send method, it is assigned to the label attribute of the same name as the message. Of course, we can customize a message object to pass in the send method.

public void Send(object obj);public void Send(object obj, MessageQueueTransaction transaction);public void Send(object obj, string label);

5. receive messages. Similarly, messages can be received in transactions. If the receive method is used to retrieve MSMQ messages, the corresponding message of MSMQ will be deleted if it is successful, only messages in the queue can be obtained.

public Message Receive();public Message Receive(MessageQueueTransaction transaction);public Message Receive(TimeSpan timeout);

If we want to retrieve the message with the specified identifier, we use the following method. ID indicates the unique identifier of the message.

public Message ReceiveById(string id);public Message ReceiveById(string id, MessageQueueTransaction transaction);

What if we don't want to delete the message in the MSMQ queue after receiving the message? The following method is used because the two methods receive MSMQ messages and do not delete the corresponding messages in MSMQ. Therefore, they do not support transactions, that is, they do not provide transaction parameters.

public Message Peek();public Message PeekById(string id);

We can also retrieve all the messages in the queue at one time.

public Message[] GetAllMessages();
Instance

After talking about this, let's take a look at the code. We use the console program for testing. I encapsulated the MSMQ queue in a simple way, as shown below:

View code

Our user entities are also very simple, as follows:

View code

Next we will create a queue. We have successfully created a "myqueue" queue.

View code

Next we will send messages to the queue ., From the right of the figure, we can see that the message is successfully added to the queue.

View code

Then we use the peek method to receive messages (that is, do not remove the corresponding message of MSMQ). Obviously, the message in the figure still exists in the MSMQ queue.

View code

Then we use the receive method to receive messages. At this time, we can see that the original message of MSMQ has been deleted.

View code

Finally, let me test the transactional nature of MSMQ. Delete our queue and recreate it. We insert five messages into the queue consecutively, but an exception is thrown when 5th messages are inserted. If MSMQ supports transactions, the four messages sent earlier will be rolled back, the MSMQ queue should contain 0 messages.

View code

 

In addition, MSMQ adopts synchronous message sending and receiving methods. In this case, if no message exists in our message queue, what will happen if we call receive () to receive the message in this queue? The program will be blocked here until there is a message in the message queue, and the program will continue. This situation is terrible, but I am not afraid that MSMQ supports asynchronous messages. Due to limited space, I will not be much. Here I will provide a link for asynchronous operations. If you are interested, you can study it, click here

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.