Message Queue MSMQ instance-PHP source code

Source: Internet
Author: User
Tags msmq
Message Queue (MSMQ) instance message queue: a container that stores messages during message transmission. MSMQ is Microsoft's message processing technology and runs on a Microsoft Windows operating system. It is divided into user queue and system queue. User queues are divided into dedicated queues: they are not released across the network and are only available on the local computer where they reside. A dedicated queue can only be accessed by user programs that know the full path name or tag of the queue.
For message queue installation, you can choose Control Panel> add/delete Programs> add/delete Windows components.

Select "message queue" and click "details ".

If you are running Windows Server 2003, select "application Server" to access message queue.

Make sure that all options, including "msmq http support", are selected on the details page.

Click OK to exit the details page, and then click next ". Complete the installation.

  

Here we use instances to describe how to create a queue, send messages, and receive messages.

Create a queue:
Create a local queue: @ ". \ private $ \ queue name"
Create a remote queue: @ "FormatName: DIRECT = TCP: remote machine IP \ private $ \ queue name"

Instantiate a Message Queue

/////////Is it a local machine?Public MSMQManager (bool isLocalComputer) {if (isLocalComputer) {_ path = @ ". \ private $ \" + (ConfigurationManager. deleetmanager ["MSMQName"]? "CSMSMQ");} else {_ path = @ "FormatName: DIRECT = TCP: 192.168.1.125 \ private $ \" + (ConfigurationManager. deleettings ["MSMQName"]? "CSMSMQ");} _ msmq = new MessageQueue (_ path );}

Create a queue

////// Create a queue //////Enable transaction?///
 Public bool Create (bool transactional) {if (MessageQueue. Exists (@ ". \ private $ \" + (ConfigurationManager. deleettings ["MSMQName"]? "CSMSMQ") {return true;} else {if (MessageQueue. Create (@ ". \ private $ \" + (ConfigurationManager. etettings ["MSMQName"]? "CSMSMQ"), transactional )! = Null) {return true;} else {return false ;}}}

Send Message Queue

////// Send a message queue //////Message Queue entity///
 Public void Send (MSMQIndex msmqIndex) {_ msmq. Send (new Message (msmqIndex, new BinaryMessageFormatter ()));}

Receive message queue, delete queue

////// Receive message queue, delete queue //////
 Public MSMQIndex ReceiveAndRemove () {MSMQIndex msmqIndex = null; _ msmq. formatter = new BinaryMessageFormatter (); Message msg = null; try {msg = _ msmq. receive (new TimeSpan (0, 0, 1);} catch (Exception ex) {Console. writeLine (ex. message);} if (msg! = Null) {msmqIndex = msg. Body as MSMQIndex;} return msmqIndex ;}

The above code roughly describes how to create, send, and receive a message queue. The following describes all the code of the message queue manager. A demo is provided in the attachment. after you have installed the message queue, the DEMO can run normally, and it is helpful to those who need it.

Message queue manager

Using System; using System. Collections. Generic; using System. Text; using System. Messaging; using System. Configuration; namespace MSMQTest. Model {////// Message queue manager // prepared by: Xinhai giant Lan xinhaijulan@gmail.com ///Public class MSMQManager: IDisposable {# region field and attribute private MessageQueue _ msmq = null; private string _ path; private static MSMQManager _ instanceLocalComputer = new MSMQManager (true );////// Local message queue instance ///Public static MSMQManager InstanceLocalComputer {get {return MSMQManager. _ instanceLocalComputer ;}} private static MSMQManager _ instance = new MSMQManager (false );////// Remote Message queue instance ///Public static MSMQManager Instance {get {return MSMQManager. _ instance ;}# endregion ////// Create a queue //////Enable transaction?///
 Public bool Create (bool transactional) {if (MessageQueue. Exists (@ ". \ private $ \" + (ConfigurationManager. deleettings ["MSMQName"]? "CSMSMQ") {return true;} else {if (MessageQueue. Create (@ ". \ private $ \" + (ConfigurationManager. etettings ["MSMQName"]? "CSMSMQ"), transactional )! = Null) {return true;} else {return false ;}}}////// Instantiate a message queue //////Is it a local machine?Public MSMQManager (bool isLocalComputer) {if (isLocalComputer) {_ path = @ ". \ private $ \" + (ConfigurationManager. deleetmanager ["MSMQName"]? "CSMSMQ");} else {_ path = @ "FormatName: DIRECT = TCP: 192.168.1.125 \ private $ \" + (ConfigurationManager. deleettings ["MSMQName"]? "CSMSMQ");} _ msmq = new MessageQueue (_ path );}////// Send a message queue //////Message Queue entity///
 Public void Send (MSMQIndex msmqIndex) {_ msmq. Send (new Message (msmqIndex, new BinaryMessageFormatter ()));}////// Receive message queue, delete queue //////
 Public MSMQIndex ReceiveAndRemove () {MSMQIndex msmqIndex = null; _ msmq. formatter = new BinaryMessageFormatter (); Message msg = null; try {msg = _ msmq. receive (new TimeSpan (0, 0, 1);} catch (Exception ex) {Console. writeLine (ex. message);} if (msg! = Null) {msmqIndex = msg. Body as MSMQIndex;} return msmqIndex;} # region IDisposable Members public void Dispose () {if (_ msmq! = Null) {_ msmq. Close (); _ msmq. Dispose (); _ msmq = null ;}# endregion }}

This DEMO only introduces and uses message queues, and some concepts of message queues are not described. for example:

Message Queue creation method: Path, format name, and tag;

Message Queue receiving method: synchronous and asynchronous;

Message Queue serialization: character, XML, binary;

And whether to use transactions.

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.