在 .NET Framework 2.0 中使用 MessageQueue






3.44/5 (6投票s)
提供对消息队列服务器上队列的访问。
引言
线程安全
只有以下方法是安全的,适用于多线程操作:BeginPeek、BeginReceive
、EndPeek
、EndReceive
、GetAllMessages
、Peek
和 Receive
。我将在我的文章中尝试解释这些方法。
备注
消息队列技术允许在不同时间的应用程序跨越可能暂时离线的异构网络和系统进行通信。应用程序从队列发送、接收或查看(读取但不移除)消息。消息队列是 Windows 2000 和 Windows NT 的一个可选组件,必须单独安装。
MessageQueue
类是消息队列的一个包装器。消息队列有多个版本,使用 MessageQueue
类可能会产生略微不同的行为,具体取决于您使用的操作系统。有关每个消息队列版本特定功能的信息,请参阅 MSDN 中 Platform SDK 的“消息队列新增功能”主题。
MessageQueue
类提供对消息队列的引用。您可以在 MessageQueue
构造函数中指定一个路径来连接到现有资源,或者在服务器上创建一个新队列。在调用 Send
、Peek
或 Receive
之前,您必须将 MessageQueue
类的新实例与现有队列关联起来。此时,您可以操作队列的属性,如 Category
和 Label
。
MessageQueue
类支持两种类型的消息检索:同步和异步。同步方法 Peek
和 Receive
会导致进程线程等待指定的间隔时间,直到新消息到达队列。异步方法 BeginPeek
和 BeginReceive
允许主应用程序任务在单独的线程中继续执行,直到消息到达队列。这些方法通过使用回调对象和状态对象在线程之间通信信息来工作。
当您创建 MessageQueue
类的新实例时,您并没有创建一个新的消息队列。相反,您可以使用 Create
、Delete
和 Purge
方法来管理服务器上的队列。
注意:与
Purge
不同,Create
和Delete
是static
(在 Visual Basic 中为Shared
)成员,因此您可以在不创建MessageQueue
类新实例的情况下调用它们。
您可以使用三种名称之一来设置 MessageQueue
对象的 Path
属性:友好名称、FormatName
或 Label
。友好名称由队列的 MachineName
和 QueueName
属性定义,对于公有队列是 _MachineName\ QueueName_,对于私有队列是 _MachineName\ Private$\ QueueName_。FormatName
属性允许离线访问消息队列。最后,您可以使用队列的 Label
属性来设置队列的 Path
。
有关 MessageQueue
实例的初始属性值列表,请参阅 MessageQueue
构造函数。
使用消息队列
创建 MessageQueue
类的实例。在创建它之前,您需要导入该类的引用。
Imports System.Messaging
'' Creating Instance of Message Queue
'' I have passed in the path Of the Queue
'' the path declared is a private queue path
Private WithEvents myQueue As New MessageQueue(".\private$\myQueue")
'' You can define the path later on also
Private WithEvents myQueue As New MessageQueue()
'' then in the form load or any other event define the path
'' the path declared is a private queue path
myQueue.Path = "".\private$\myQueue"
我声明了消息队列并使用了事件,因为我将使用该类提供的一些事件。
现在,让我们看看如何将消息发送到队列。
要将消息发送到消息队列,您可以创建一个使用 Message
类表示的消息,或者直接发送一个字符串。
'' sending by creating a message variable
Dim msg As New Message
'' it is the lable of the message
msg.Label = "Example"
'' it is the body of the message
msg.Body = "This message is send as an example"
myQueue.Send(msg)
'' Send msg with creating a message vairable
myQueue.Send("Example","This message is send as an example")
还有一些其他方法可以将消息发送到队列。我在 MSDN 上找到了这些方法。我发现其中一些方法非常有用。
' References public queues.
Public Sub SendPublic()
'' the path is of public queue
Dim myQueue As New MessageQueue(".\myQueue")
myQueue.Send("Public queue by path name.")
Return
End Sub 'SendPublic
' References private queues.
Public Sub SendPrivate()
Dim myQueue As New MessageQueue(".\Private$\myQueue")
myQueue.Send("Private queue by path name.")
Return
End Sub 'SendPrivate
' References queues by label.
Public Sub SendByLabel()
Dim myQueue As New MessageQueue("Label:TheLabel")
myQueue.Send("Queue by label.")
Return
End Sub 'SendByLabel
' References queues by format name.
Public Sub SendByFormatName()
Dim myQueue As New _
MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4-935C-845C2AFF7112")
myQueue.Send("Queue by format name.")
Return
End Sub 'SendByFormatName
现在,让我们看看如何从队列接收消息。
通过使用 Receive
方法,我们可以接收队列中的第一条消息,并将其从队列中移除。
'' using the recieve method
myQueue.Receive()
但是有一个窍门:我们必须将收到的消息存储在 Message
变量中。
''saving message from the queue in our temp message variable
Dim tempMsg as New Message
tempMsg = myQueue.Receive()
如果您想在移除消息之前先检查一下,可以使用 Peek
方法。
'' using the peek method
Dim tempMSg as New Message
tempMsg = MyQueue.Peek()
'' Check if the message is ours
If tempMsg.Label = "Example" Then
'' we got the message
'' so we remove it from the Queue
myQueue.Receive()
End If
关注点
当有两个应用程序需要共享数据时,MessageQueue
非常有用。
参考文献
我通过在网上和 MSDN 上查找一些示例来撰写本文。