The previous article, on the MSMQ installation under WinCE, describes the development of MSMQ under the. NET Compact Framework.
The so-called MQ is message queue. Message Queuing can serve as a conduit for communication between different applications, or even between different machines. The content of the communication under Message Queuing is called a message, and in the C # program the messages are the objects.
MSMQ is the MQ service program provided by Microsoft. The MQ service is responsible for managing Message Queuing, ensuring that messages are sent to the right end in the channel of Message Queuing, MQ supports offline transactions, and sometimes messages are cached in MQ service programs, and when the receiver is picking up messages at the time of the line. This feature allows MQ to be widely used in the mobile world because the network of mobile applications does not guarantee 7x24 long connections.
Build queues
To develop MQ under Cf.net, you need to refer to the System.Messaging library.
using System.Messaging;
public class Mqservice
{
Private Const string mmachineprefix = @ ". \";
Private Const String Mprivatequeuenameprefix = Mmachineprefix + @ "private$\";
Private Const String Mservicequeuepath = Mprivatequeuenameprefix + "mqservicequeue$";
private MessageQueue Mservicequeue;
private void Initservicequeue ()
{
//Create the message queue
Try
{
//Check to make sure the message queue does not exist already
if (! Messagequeue.exists (Mservicequeuepath))
{
//Create the new message queue and make it transactional
mservicequeue = messagequeue.create (Mservicequeuepath);
Mservicequeue.close ();
}
Else
{
mservicequeue = new MessageQueue (Mservicequeuepath);
}
type[] types = new Type[1];
Types[0] = typeof (String);
mservicequeue.formatter = new XMLMessageFormatter (types);
mservicequeue.receivecompleted + = new Receivecompletedeventhandler (Messagelistenereventhandler);
//Begin the asynchronous receive operation.
mservicequeue.beginreceive ();
Mservicequeue.close ();
}
//Show message if we used a invalid message queue name;
catch (messagequeueexception mqexception)
{
Console.WriteLine (mqexception.message);
}
return;
}
}
using System.Messaging;
public class Mqservice
{
Private Const string mmachineprefix = @ "."
Private Const String Mprivatequeuenameprefix = Mmachineprefix + @ "private$";
Private Const String Mservicequeuepath = Mprivatequeuenameprefix + "mqservicequeue$";
private MessageQueue Mservicequeue;
private void Initservicequeue ()
{
//Create the message queue
Try
{
//Check to make sure the message queue does not exist already
if (! Messagequeue.exists (Mservicequeuepath))
{
//Create the new message queue and make it transactional
mservicequeue = messagequeue.create (Mservicequeuepath);
Mservicequeue.close ();
}
Else
{
mservicequeue = new MessageQueue (Mservicequeuepath);
}
type[] types = new Type[1];
Types[0] = typeof (String);
mservicequeue.formatter = new XMLMessageFormatter (types);
mservicequeue.receivecompleted + = new Receivecompletedeventhandler (Messagelistenereventhandler);
//Begin the asynchronous receive operation.
mservicequeue.beginreceive ();
Mservicequeue.close ();
}
//Show message if we used a invalid message queue name;
catch (messagequeueexception mqexception)
{
Console.WriteLine (mqexception.message);
}
return;
}
}