微软的 MSMQ (MessageQueue)的使用,一些理解,以及尝试 |
您所在的位置:网站首页 › 消息队列并发问题是什么意思 › 微软的 MSMQ (MessageQueue)的使用,一些理解,以及尝试 |
电脑MESSAGE信息
MSMQ 1.建立MQ服务 MessageQueue myQueue = new MessageQueue(string path); Path路径格式
Queue type Syntax
Public queue MachineName\QueueName 公用队列 Private queue MachineName\Private$\QueueName 专用队列 Journal queue MachineName\QueueName\Journal$
Machine journal queue MachineName\Journal$ 系统队列--日志 Machine dead-letter queue MachineName\Deadletter$ 系统队列--死信信息 Machine transactional dead-letter queue MachineName\XactDeadletter$ 系统队列--事务性死信信息 个人添加
Label Label:TheLabel 利用标签,写入(标签唯一),目前试出公共队列可以使用标签,其他使用不了 FormatName 1.FormatName:Direct=TCP:ip\\private$\\myqueue 2.FormatName:Direct=OS:machinename\\private$\\queuename 3.FormatName:DIRECT=http://222.10.xx.xx/msmq/Private$/test
1.FontName是区分大小写的 2.如果远程服务器为域中的服务器则可使用Direct=OS:machinename\Private$\...的形式发送消息。 3.如果远程服务器为非域中的服务器,则可以使用TCP或http的形式发送。
5.在代码中创建的队列实例对象的事务性属性,必须与要发送的目标队列的属性相匹配。前面的例子中发送的消息为非事务型消息,如果要发送消息到事务型的队列,代码为: MessageQueue rmTxnQ = new MessageQueue ("FormatName:Direct=OS:machinename\\private$\\queue"); rmTxnQ.Send("sent to Txn queue - Atul", MessageQueueTransactionType.Single);
如果事务型属性不匹配,消息将无法传递。系统不会返回任何错误,但该条消息却会丢掉。 MessageQueueTransactionType // Operation will not be transactional. None = 0, // // Summary: // A transaction type used for Microsoft Transaction Server (MTS) or COM+ 1.0 // Services. If there is already an MTS transaction context, it will be used // when sending or receiving the message. Automatic = 1, // // Summary: // A transaction type used for single internal transactions. Single = 3,
2.通过MQ传出的信息,如果远程MQ服务器,断掉了。如图中,系统的传出队
对于队列信息,可以手动清除
标签
5.MessageQueue.Receive();这个接收MSMQ信息的方法,是实时的 需要
MessageQueue queueJournal = new MessageQueue(".\\myQueue"); while (true) { Message journalMessage = queueJournal.Receive(); // Process the journal message. journalMessage.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) }); if (journalMessage != null) Console.WriteLine("" + journalMessage.Body); }
//new Message(object body); //赋值 body //更改body 信息 // m.Body = "HY_Public queue by path name.";
Message.Formatter 解析body的格式
Gets or sets the formatter used to serialize an object into or deserialize an object from the message body.
MessageQueue.Send发送信息到MSMQ MessageQueue.receive实时接收信息
8.获取MQ数据方法 a. 声明 MessageQueue myQueue = new MessageQueue(".\\myAsyQueue"); b. //这里使用了委托,当接收消息完成的时候就执行MyReceiveCompleted方法 myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted);
myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
3. 等待接收第一个MQ信息 //通知一个或多个正在等待的线程已发生事件 3.1类声明变量 static ManualResetEvent signal = new ManualResetEvent(false);
3.2 MessageQueueTransaction myTransaction = new MessageQueueTransaction();//用途不明确
myTransaction.Begin(); myQueue.BeginReceive();//启动一个没有超时时限的异步操作
signal.WaitOne();//等待接收线程的信息
myTransaction.Commit(); 3.3 方法 private static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult) { try { MessageQueue myQueue = (MessageQueue)source;
myQueue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) }); //完成指定的异步接收操作
Message message = myQueue.EndReceive(asyncResult.AsyncResult); //处理信息 signal.Set();//设置线程信息
string book = message.Body.ToString(); Console.WriteLine("收到" + book);
myQueue.BeginReceive();//开始接收下个信息
}
catch (MessageQueueException me) {
} }
获取或设置一个值,该值指示是否由应用程序维护连接缓存。 public static bool EnableConnectionCache { get; set; }
1.peek方法(获取信息,但是不移除) Peek() 返回,但不移除 (查看) 在队列中的第一个消息引用的 MessageQueue。 Peek 方法是同步的因此阻止当前线程,直至有消息变为可用。
2.reflash方法 Refresh 同步的属性 MessageQueue 与其相关联的消息队列服务器资源。 如果任何属性,如 Label 或 Category, ,次后,在服务器上发生更改 MessageQueue 已创建,请 Refresh 更新 MessageQueue 使用新的信息。 4.delete方法 删除消息队列服务器上的队列。
6. 清除某个队列的数据 MessageQueue myQueue = new MessageQueue(".\\myAsyQueue"); myQueue.Purge(); |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |