来源: MSMQ 的持久化_走错路的程序员的博客-CSDN博客
MSMQ的消息默认是放在内存里面的. 重启服务或者断电的时候消息就没了. 对于重要的消息来讲这样肯定是不行的.
百度了好久也没发现如何持久化. 后来实在不行上Google 才找到对应的答案.
废话少说. 直接上答案.
标准答案就一句话 msg1.Recoverable = true;
System.Messaging.Message msg1 = new System.Messaging.Message(person);
msg1.Recoverable = true;//为true 的时候进行持久化.每个信息的Recoverable 为true 那么持久保存
mq.Send(msg1, transaction);
//下面的是全部的测试代码.. 可以不看..
//主测试方法
public class TestMQ
{
static string path = “.\\private$\\TestMQ”;
// 接收消息的函数
public static Object[] Receive()
{
MessageQueue mq;
if (MessageQueue.Exists(path))
{
mq = new MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path);
}
// 定义消息队列中所有的消息种类(种类之间的顺序可以互换)
Type[] msgTypes = new Type[] { typeof(Person), typeof(Order), typeof(String) };
// 规定消息以XML格式编码
mq.Formatter = new XmlMessageFormatter(msgTypes);
// 定义事务
MessageQueueTransaction transaction = new MessageQueueTransaction();
try
{
// 如果消息队列采用了事务,则开始事务
if (mq.Transactional)
transaction.Begin();
System.Messaging.Message msg1 = mq.Receive(transaction);
Person person = (Person)msg1.Body;
System.Messaging.Message msg2 = mq.Receive(transaction);
Order order = (Order)msg2.Body;
System.Messaging.Message msg3 = mq.Receive(transaction);
string description = (String)msg3.Body;
// 如果消息队列采用了事务,则停止事务
if (mq.Transactional)
transaction.Commit();
return new Object[] { person, order, description };
}
catch (Exception ex)
{
// 如果消息队列采用了事务并且出现了异常,则终止事务
if (mq.Transactional)
transaction.Abort();
return null;
}
}
// 发送消息的函数
public static bool Send(Person person, Order order, string description)
{
MessageQueue mq;
if (MessageQueue.Exists(path))
{
mq = new MessageQueue(path);
}
else
{
mq = MessageQueue.Create(path);
}
mq.DefaultPropertiesToSend.Recoverable = true; //为true 的时候进行持久化. 经测试好像没用.
//mq.DefaultPropertiesToSend.AcknowledgeType = AcknowledgeTypes.NegativeReceive; 消息是否需要反馈.
// 规定消息以XML格式编码
mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(Person), typeof(Order), typeof(String) });
// 定义事务
MessageQueueTransaction transaction = new MessageQueueTransaction();
System.Messaging.Message msg1 = new System.Messaging.Message(person);
System.Messaging.Message msg2 = new System.Messaging.Message(order);
System.Messaging.Message msg3 = new System.Messaging.Message(description);
try
{
// 如果消息队列采用了事务,则开始事务
if (mq.Transactional)
transaction.Begin();
msg1.Recoverable = true;//为true 的时候进行持久化.
mq.Send(msg1, transaction);
msg2.Recoverable = true;//为true 的时候进行持久化.
mq.Send(msg2, transaction);
msg3.Recoverable = false;//为true 的时候进行持久化.
mq.Send(msg3, transaction);
// 如果消息队列采用了事务,则停止事务
if (mq.Transactional) {
transaction.Commit();
}
return true;
}
catch (Exception ex)
{
// 如果消息队列采用了事务并且出现了异常,则终止事务
if (mq.Transactional)
transaction.Abort();
return false;
}
finally
{
mq.Close();
msg1 = null;
msg2 = null;
msg3 = null;
}
}
}
下面是两个model类
// 购物清单
public class Order
{
public Order()
{
Price = 0.0;
Number = 0;
Time = DateTime.Now;
}
public Order(Double price, UInt64 number, DateTime time)
{
Price = price;
Number = number;
Time = time;
}
// 物品单价
Double Price;
// 物品数量
UInt64 Number;
// 下单时间
DateTime Time;
}
// 人员类
public class Person
{
public Person()
{
m_Name = “”;
m_Age = 0;
}
public Person(string name, UInt16 age)
{
m_Name = name;
m_Age = age;
}
// 姓名
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
// 年龄
public UInt16 Age
{
get { return m_Age; }
set { m_Age = value; }
}
private string m_Name;
private UInt16 m_Age;
}
//发送按钮
private void 发送按钮_Click(object sender, EventArgs e)
{
for (int i = 0; i < 100000; i++)
{
Person person = new Person(“Jackie”, 30);
Order order = new Order(110.0, 10, DateTime.Now);
string description = “This is a new order.”;
TestMQ.Send(person, order, description);
System.GC.Collect();
}
}
private void 接收按钮_Click(object sender, EventArgs e)
{
// 接收消息。返回值为 null 时表示接收失败
Object[] message = TestMQ.Receive();
if (message != null)
{
// 当消息接收成功时,将消息依次取出并存入下面的对象中
Person person = (Person)message[0];
Order prder = (Order)message[1];
string description = (string)message[2];
if (description == “This is a new order.”)
{
MessageBox.Show(“消息接收成功 !”);
}
else
{
MessageBox.Show(“消息接收失败 !”);
}
}
else
{
MessageBox.Show(“消息接收失败 !”);
}
}
————————————————
版权声明:本文为CSDN博主「走错路的程序员」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/phker/article/details/71124256