This article is a mirror article of machine translation, please click here to jump to the original article.

View: 304494|Reply: 72

[Source] c# The use of the message queue MessageQueue with source code

  [Copy link]
Posted on 12/14/2016 1:51:37 PM | | | |

With MSMQ (Microsoft Message Queue), application developers can conveniently communicate with applications quickly and reliably by sending and receiving messages. Message processing provides you with a reliable fail-safe method for guaranteed messaging and performing many business processes.

MSMQ, like XML Web Services and .Net Remoting, is a distributed development technology. However, when using XML Web Services or .Net Remoting components, the client needs to exchange information with the server in real time, and the server needs to stay online. MSMQ can work when the server is offline, temporarily saving the message in the message queue on the client side, and then sending it to the server for processing when it is online.

Obviously, MSMQ is not suitable for the situation where the client needs to respond in a timely manner from the server, and MSMQ interacts with the server in an asynchronous way, so there is no need to worry about waiting for the server to process for a long time.



Although both XML Web Services and .Net Remoting provide the [OneWay] property to handle asynchronous calls, it is used to solve the problem of long method calls on the server side blocking the client side for a long time. However, it cannot solve the problem of large client load, and the server accepts requests faster than it can process.

In general, the [OneWay] attribute is not used in specialized messaging services.



1. Basic terms and concepts

A "message" is a unit of data that is transmitted between two computers. Messages can be very simple, such as containing only text strings; It can also be more complex and may contain embedded objects.



Messages are sent to the queue. A "message queue" is a container that holds messages during their transmission. The message queue manager acts as an intermediary when relaying messages from its source to its destination. The main purpose of a queue is to provide routing and guarantee the delivery of messages; If the recipient is unavailable when the message is sent, the message queue holds the message until it can be successfully delivered.



"Message Queuing" is Microsoft's message processing technology that provides message processing and message queuing capabilities for any application in any combination of computers with Microsoft Windows installed, regardless of whether they are on the same network or online at the same time.



A "message queue network" is any group of computers that can send messages back and forth to each other. Different computers in a network play different roles in ensuring that messages are processed smoothly. Some of them provide routing information to determine how to send messages, some hold important information about the entire network, and some simply send and receive messages.



During Message Queue installation, administrators determine which servers can communicate with each other and set special roles for specific servers. The computers that make up this network of "message queues" are called "sites," and they are connected to each other by "site links." Each sitelink has an associated "overhead" that is determined by the administrator and indicates how often messages are delivered through this sitelink.



The Message Queue administrator also sets up one or more computers in the network that act as Routing Servers. The routing server looks at the overhead of each sitelink to determine the fastest and most efficient way to deliver a message through multiple sites to determine how to deliver the message.



2. Queue Type

There are two main types of queues: queues created by you or other users in your network and system queues.

A user-created queue can be any of the following:

Public queues are replicated across the entire Message Queue network and have the potential to be accessed by all sites connected to the network.

Private queues are not published across the network. Instead, they are only available on the local computer where they reside. Private queues can only be accessed by applications that know the full path name or label of the queue.

The Management Queue contains messages that acknowledge message receipts sent in a given Message Queue network. Specify the management queue you want the MessageQueue component to use, if any.

The Response Queue contains the response messages that are returned to the sending application when the target application receives the message. Specify the response queue you want the MessageQueue component to use, if any.



The queues generated by the system are generally divided into the following categories:

Journal Queue optionally stores copies of sent messages and copies of messages removed from the queue. A single journal queue on each Message Queue client stores a copy of messages sent from that computer. A separate diary queue is created for each queue on the server. This journal tracks messages that are removed from that queue.

The Dead Letter Queue stores copies of messages that cannot be delivered or have expired. If a message that expires or cannot be delivered is transactional, it is stored in a special dead-letter queue called a "transactional dead-letter queue". Dead letters are stored on the computer where the expired message resides. For more information about timeout and expiration messages, see Default message properties.

The Report Queue contains messages that indicate the route the message takes to reach the destination, and can also contain test messages. There can only be one reporting queue on each computer.

A dedicated system queue is a series of dedicated queues that store the management and notification messages that a system needs to perform message processing operations.

Most of the work done in the application involves accessing public queues and their messages. However, depending on the application's journaling, acknowledgment, and other special processing needs, it is likely that several different system queues will be used in daily operations.



3. Synchronous VS. Asynchronous Communication

Queue communication is inherently asynchronous because sending messages to and receiving messages from the queue is done in different processes. Also, the receive operation can be performed asynchronously, because the person who wants to receive the message can call the BeginReceive method to any given queue and immediately continue with other tasks without waiting for a reply. This is very different from what people know as "synchronous communication".



In synchronous communication, the sender of the request must wait for a response from the intended receiver before performing other tasks. The amount of time the sender waits depends entirely on the time it takes for the receiver to process the request and send the response.



4. Interacting with Message Queues

Message processing and messaging provide a powerful and flexible mechanism for inter-process communication between server-based application components. They offer several advantages over direct invocation between components, including:

Stability – Component failures affect messages much less than direct calls between components, as messages are stored in a queue and remain there until they are properly processed. Message processing is similar to transaction processing in that message processing is guaranteed.
Message prioritization – More urgent or important messages can be received before relatively unimportant messages, so you can guarantee adequate response time for critical applications.
Offline capability – When messages are sent, they can be sent to a temporary queue and remain there until they are successfully delivered. When access to the desired queue is unavailable for any reason, the user can proceed with the action. At the same time, other operations can continue as if the message had been processed, because message delivery is guaranteed when the network connection is restored.
Transactional message processing – Couple multiple related messages into a single transaction to ensure that messages are delivered sequentially, only once, and can be retrieved successfully from their destination queue. If there are any errors, the entire transaction is canceled.
Security – The message queuing technology on which the MessageQueue component is based uses Windows Security to secure access control, provide auditing, and encrypt and authenticate messages sent and received by the component.


5. Write a simple message queue program in the .Net environment

(1) Install Message Queuing Services first

Install MSMQ through the Control Panel, the "Add/Remove Programs" – "Add/Remove Windows Components" step.

MSMQ can be installed in either workgroup mode or domain mode. If the installer does not find a server running a message queue that provides directory services, it can only be installed in workgroup mode, and the Message Queue on this computer only supports the creation of private queues and the creation of direct connections to other computers running Message Queues.



(2) Configure MSMQ

Open Computer Management – Message Queuing and create an MSMQDemo queue under Private Queues


(3) Write code - simply demonstrate the MSMQ object

The MessageQueue class is a wrapper around a "message queue". The MessageQueue class provides a reference to the Message Queue queue. You can specify a path to an existing resource in the MessageQueue constructor, or you can create a new queue on the server. Before you can call Send, Peek, or Receive, you must associate a new instance of the MessageQueue class with an existing queue.



MessageQueue supports two types of message retrieval: synchronous and asynchronous. The synchronized Peek and Receive methods cause the process thread to wait for a new message to arrive at the queue at a specified interval. The asynchronous BeginPeek and BeginReceive methods allow the main application task to continue executing in a separate thread before the message reaches the queue. These methods work by using callback objects and state objects to facilitate information communication between threads.


Source code download:

Tourists, if you want to see the hidden content of this post, pleaseReply





Previous:15 good. .NET
Next:c#/.net reads the source code of the hash information of the torrent torrent
Posted on 6/13/2021 8:54:46 PM |
OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK OK
Posted on 2/22/2019 11:01:34 AM |
https://download.csdn.net/download/winrich/9585398
Posted on 7/21/2021 11:20:26 AM |
Good, good
Posted on 12/21/2016 11:04:53 PM |
Thank you, landlord
Posted on 12/28/2016 7:20:26 PM |
Check out this current one to do this exercise
Posted on 7/14/2017 4:32:06 PM |
Thank you, landlord。。。。。。。
Posted on 10/18/2017 2:26:45 PM |
Learn from the group leaders
Posted on 10/24/2017 5:39:19 AM |
Learn it
Posted on 11/18/2017 9:56:57 PM |
GOOOOOOOOOOOOOOOD
Posted on 12/14/2017 2:04:20 PM |
Good posts must be replied
Posted on 12/16/2017 9:49:55 PM |
213123saedwaeas
Posted on 12/17/2017 8:56:11 AM |
Look at what is being said
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com