C # Message Queuing-msmq

Source: Internet
Author: User
Tags msmq


MQ is a messaging middleware technology, so it can support many types of language development, but also a cross-platform communication mechanism, that is, MQ supports the conversion of information into XML or JSON and other types of data storage into the message queue, and then can use different languages to process messages in the message queue, This makes it easy to communicate information, but also for the information of the communication played a role in buffering, often in financial projects using this communication mechanism.

1 MQ Installation

Open Control Panel-Add/Remove Programs – Add/Remove Windows Components step to install MSMQ.

MSMQ can be installed as 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 as a workgroup mode, and Message Queuing on this computer only supports creating private queues and creating direct connections to other computers running Message Queuing.

2 Configuring MSMQ

Turn on Computer Management – services and applications – Message Queuing, creating Msmqdemo queues under private queues

3 MQ Demo

In. NET, Microsoft encapsulates MQ, encapsulating MQ-related information into the MessageQueue class, which can be referenced directly at development time to manipulate messages in the queue.
Before you manipulate a message, you first specify the stored queue for the message, so when you create the message, you first create a queue on the server, and then specify the path to the message queue for MessageQueue.

Next, you'll demonstrate how to send data and receive data code, and the following example uses a private queue type to demonstrate the operation. First, starting from the sending of data, when the data is sent first to create our MQ, and then according to the MQ address to create the appropriate queue, call the queue's Send method to send data information to the queue, the following code:

Private voidSENDMAILMQ (list<string> Addressstr,stringAccountstringPasswordstringHoststringPortstringTitlestringMessageLongTenantId) {            Try            {                //declaring an MQ path                varEKQ =". \\Private$\\mailqueue"; //created when no path exists                if(!messagequeue.exists (EKQ))                Messagequeue.create (EKQ); //Create an instance of                varQueue =NewMessageQueue (EKQ); foreach(varIteminchaddressstr) {                    varEncryptstr =Jsonconvert.serializeobject (New{MAILMSG=New{ Account=Account , password=Password, host=Host, Port=Port, Title=title, Message=message, Address=Item}, TenantId=TenantId}); Queue.                    Send (ENCRYPTSTR);                Savelog (ENCRYPTSTR); }            }            Catch(Exception ex) {Savelog ("Exception:"+ ex. Message +Ex.            StackTrace); }        }

After running the above code, MQ will send the message to the appropriate queue, which uses a proprietary queue so it sends the message to a local queue, so that a loosely coupled bridge is built between the sender and caller of the message, which is the message queue,

Next, we show you how to receive Message Queuing.

 Public classmailthread {MessageQueue queue;  Public voidstartmailmq () {stringEKQ =". \\Private$\\mailqueue"; //created when no path exists            if(!messagequeue.exists (EKQ))            Messagequeue.create (EKQ); Queue=NewMessageQueue (EKQ); Queue. Formatter=NewXMLMessageFormatter (NewType[] {typeof(String)});  while(!messagequeue.exists (EKQ)) {Console.WriteLine ("mailqueue queue not found"); } queue. ReceiveCompleted+=queue_receivecompleted; Queue.        BeginReceive (messagequeue.infinitetimeout); }        Private voidQueue_receivecompleted (Objectsender, Receivecompletedeventargs e) {            varm =E.message; varDeceivedmsg =m.body.tostring (); Console.WriteLine ("Receive Message message contents: {0} \ n--------------", (string) m.body); if(!string. IsNullOrEmpty (deceivedmsg))NewDispatchmessager ().                        SendMail (DECEIVEDMSG); Queue.        BeginReceive (messagequeue.infinitetimeout); }    }
End


MQ is an Enterprise Service message Intermediate section technology, which often accompanies the use of Enterprise service Bus, constitutes a part of the enterprise distributed development, if it is possible to consider the transmission and delivery of messages between the communication is not mutually connected and requires a distributed architecture, you can consider using MQ to do the message of the intermediate price technology, The capabilities of MQ are sufficient for development use.

C # Message Queuing-msmq

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.