MessageQueue .NET进程通信初探
最近写了写了一个小程序,有关于.net进程通信的;以前根本没接触过,还好查了一些资料后;稍微有点明白。对.NET通信也有了一定了解。闲话少说,具体的心得慢慢总结,以后再贴出来。
为模拟两个进程间通信;我在一个solution下见了3个console application;(为区别事务队列和一般队列);
主程序:
Code
using System;
using System.Messaging;
namespace MyProject
{
public class MyNewQueue
{
public static void Main()
{
MyNewQueue myNewQueue = new MyNewQueue();
Console.WriteLine("Start ReceivMessage and Press Any key to Start!");
Console.ReadLine();
myNewQueue.ReceivMessage();
Console.WriteLine("Then start SendMessageTransation and Press Any key to Start!");
Console.ReadLine();
myNewQueue.ReceivMessageTransation();
Console.WriteLine("Sucess and press any key to exit!");
Console.ReadLine();
}
public void ReceivMessage()
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueue"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueue");
}
MessageQueue queue = new MessageQueue(@".\Private$\TestPQueue");
queue.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
using (MessageEnumerator messages = queue.GetMessageEnumerator2())
{
while (messages.MoveNext(TimeSpan.FromSeconds(30)))
{
Message message = messages.Current;
message = queue.Receive();
Console.WriteLine(message.Body);
}
}
return;
}
public void ReceivMessageTransation()
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueueTransation3"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueueTransation3", true);
}
MessageQueue queue2 = new MessageQueue(@".\Private$\TestPQueueTransation3");
queue2.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
MessageQueueTransaction mytransaction = new MessageQueueTransaction();
try
{
using (MessageEnumerator messages = queue2.GetMessageEnumerator2())
{
while (messages.MoveNext(TimeSpan.FromSeconds(30)))
{
mytransaction.Begin();
Message message = messages.Current;
message = queue2.Receive(mytransaction);
Console.WriteLine(message.Body);
mytransaction.Commit();
}
}
}
catch (MessageQueueException e)
{
if (e.MessageQueueErrorCode ==MessageQueueErrorCode.TransactionUsage)
{
Console.WriteLine("Queue is not transactional.");
}
mytransaction.Abort();
}
return;
}
}
}
using System;
using System.Messaging;
namespace MyProject
{
public class MyNewQueue
{
public static void Main()
{
MyNewQueue myNewQueue = new MyNewQueue();
Console.WriteLine("Start ReceivMessage and Press Any key to Start!");
Console.ReadLine();
myNewQueue.ReceivMessage();
Console.WriteLine("Then start SendMessageTransation and Press Any key to Start!");
Console.ReadLine();
myNewQueue.ReceivMessageTransation();
Console.WriteLine("Sucess and press any key to exit!");
Console.ReadLine();
}
public void ReceivMessage()
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueue"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueue");
}
MessageQueue queue = new MessageQueue(@".\Private$\TestPQueue");
queue.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
using (MessageEnumerator messages = queue.GetMessageEnumerator2())
{
while (messages.MoveNext(TimeSpan.FromSeconds(30)))
{
Message message = messages.Current;
message = queue.Receive();
Console.WriteLine(message.Body);
}
}
return;
}
public void ReceivMessageTransation()
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueueTransation3"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueueTransation3", true);
}
MessageQueue queue2 = new MessageQueue(@".\Private$\TestPQueueTransation3");
queue2.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
MessageQueueTransaction mytransaction = new MessageQueueTransaction();
try
{
using (MessageEnumerator messages = queue2.GetMessageEnumerator2())
{
while (messages.MoveNext(TimeSpan.FromSeconds(30)))
{
mytransaction.Begin();
Message message = messages.Current;
message = queue2.Receive(mytransaction);
Console.WriteLine(message.Body);
mytransaction.Commit();
}
}
}
catch (MessageQueueException e)
{
if (e.MessageQueueErrorCode ==MessageQueueErrorCode.TransactionUsage)
{
Console.WriteLine("Queue is not transactional.");
}
mytransaction.Abort();
}
return;
}
}
}
一般队列(SendMessage):
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace SendMessage
{
class Program
{
static void Main(string[] args)
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueue"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueue");
}
MessageQueue queue = new MessageQueue(".\\Private$\\TestPQueue");
Console.WriteLine("StartSendMessage");
for (int i = 0; i < 5000; i++)
{
queue.Send("a");
queue.Send("b");
queue.Send("c");
queue.Send("d");
queue.Send("e");
queue.Send("f");
queue.Send("g");
queue.Send("h");
queue.Send("i");
queue.Send("j");
}
Console.WriteLine("Sucess!");
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace SendMessage
{
class Program
{
static void Main(string[] args)
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueue"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueue");
}
MessageQueue queue = new MessageQueue(".\\Private$\\TestPQueue");
Console.WriteLine("StartSendMessage");
for (int i = 0; i < 5000; i++)
{
queue.Send("a");
queue.Send("b");
queue.Send("c");
queue.Send("d");
queue.Send("e");
queue.Send("f");
queue.Send("g");
queue.Send("h");
queue.Send("i");
queue.Send("j");
}
Console.WriteLine("Sucess!");
Console.ReadLine();
}
}
}
事务队列(SendMessageTransztion):
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace SendMessageTransztion
{
class Program
{
static void Main(string[] args)
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueueTransation3"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueueTransation3",true);
}
MessageQueue queue = new MessageQueue(".\\Private$\\TestPQueueTransation3");
MessageQueueTransaction transation = new MessageQueueTransaction();
try
{
transation.Begin();
Console.WriteLine("SendMessage..");
for (int i = 0;i < 5000; i++)
{
queue.Send("a", transation);
queue.Send("b", transation);
queue.Send("c", transation);
queue.Send("d", transation);
queue.Send("e", transation);
queue.Send("f", transation);
queue.Send("g", transation);
queue.Send("h", transation);
queue.Send("i", transation);
queue.Send("j", transation);
}
transation.Commit();
Console.WriteLine("Sucess");
Console.ReadLine();
}
catch
{
transation.Abort();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Messaging;
namespace SendMessageTransztion
{
class Program
{
static void Main(string[] args)
{
if (!MessageQueue.Exists(".\\Private$\\TestPQueueTransation3"))
{
MessageQueue myNewPrivateQueue =
MessageQueue.Create(".\\Private$\\TestPQueueTransation3",true);
}
MessageQueue queue = new MessageQueue(".\\Private$\\TestPQueueTransation3");
MessageQueueTransaction transation = new MessageQueueTransaction();
try
{
transation.Begin();
Console.WriteLine("SendMessage..");
for (int i = 0;i < 5000; i++)
{
queue.Send("a", transation);
queue.Send("b", transation);
queue.Send("c", transation);
queue.Send("d", transation);
queue.Send("e", transation);
queue.Send("f", transation);
queue.Send("g", transation);
queue.Send("h", transation);
queue.Send("i", transation);
queue.Send("j", transation);
}
transation.Commit();
Console.WriteLine("Sucess");
Console.ReadLine();
}
catch
{
transation.Abort();
}
}
}
}
先把代码贴出来,慢慢总结心得。