事务型消息队列的一些注意事项

1、要想队列用事务必须创建事务型队列

MessageQueue.Create(Settings.Default.MQPath, true);

2、要有相应的权限设置 ,默认只能被创建消息队列的用户使用

MessageQueue.Create(Settings.Default.MQPath, true).SetPermissions("Everyone", MessageQueueAccessRights.FullControl);

 3、使用MessageQueu类的Send方法时必须以事务提交,不然发送不出去。使用Receive时可以不用事务接收

发送:

MessageQueueTransaction myTransaction = new MessageQueueTransaction();

// Begin the transaction.

myTransaction.Begin();

// Send the message.

myNewPrivateQueue.Send("My Message Data.", myTransaction);

// Commit the transaction.

myTransaction.Commit();

使用TransactionScope事务接收:

using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 20)))

{

Message m = mq.Receive(new TimeSpan(0, 0, 10), MessageQueueTransactionType.Automatic);

Console.WriteLine(m.Body);

ts.Complete();

}

备注:在Receive时加入等待时间可以防止线程阻塞,无法Abort线程

posted on 2008-03-08 22:05 Neo Devin 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cuitao/archive/2008/03/08/1095715.html

猜你喜欢

转载自blog.csdn.net/weixin_34378922/article/details/93918020