【问题标题】:How do I send/receive multicast messages using MSMQ?如何使用 MSMQ 发送/接收多播消息?
【发布时间】:2015-04-30 15:28:17
【问题描述】:

谁能帮我找到一些示例代码,以使用您选择的任何 .NET 语言使用 MSMQ 发送和接收 多播 消息。我四处搜索,发现发送是:

MessaegQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")

我尝试用 Receive 做同样的想法:

MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Receive();

但我什么也得不到。谁能展示一些关于如何接收多播消息的示例代码?还是我发错了?

【问题讨论】:

  • 这可能会有所帮助:stackoverflow.com/questions/10435706/…
  • 我今天多次偶然发现该网站。他在那里给出的解释非常混乱,但现在完全有道理。我曾经使用 ActiveMQ 和 WebSpehere MQ,它们在主题和队列方面比 MSMQ 更简单。 MSMQ 可以做同样的事情,但需要一点工程来实现同样的事情。在我现在的公司,我们想尝试 MSMQ。

标签: c# .net vb.net msmq


【解决方案1】:

所以我想通了。

发送多播消息:

MessageQueue topic = new MessageQueue("formatname:multicast=234.1.1.1:8081")
topic.Send("Hello out there")

要接收多播消息:

这有点棘手,因为您无法订阅多播地址。您需要做的是创建一个队列,最好创建一个私有队列,该队列将附加到您要监视的多播地址,然后侦听您创建的私有队列而不是多播地址。像这样的:

  Dim privMulticastQueue As String = GetPrivateQueueForMulticastAddress("formatname:multicast=234.1.1.1:8081")
  Dim msgq as MessageQueue = GetMessageQueue(privMulticastQueue)
  msgq.MulticastAddress = GetMulticastAddress(destination)
  msgq.Label = "Private Queue for receiving messages from: " & destination
  msgq.Receive()

还有一些支持方法(可能有更好的方法来编写它们,所以请随时纠正,但这是我第一次尝试):

 Private Function GetPrivateQueueForMulticastAddress(ByVal dest As String) As String
    Dim privateQ As String = GetMulticastAddress(dest).Replace(".", "_").Replace(":", "_")
    Return ".\Private$\" & privateQ
 End Function

Private Function GetMulticastAddress(ByVal dest As String) As String
    Return dest.Split("=")(1)
End Function

Private Function GetMessageQueue(ByVal dest As String) As MessageQueue   
      Try
           If Not MessageQueue.Exists(dest) Then
             MessageQueue.Create(dest)
            End If

            Dim msgq As MessageQueue = New MessageQueue(dest)
            Return msgq
      Catch ex As Exception
            Throw New EMGException("Failed while trying to use destination: " & dest, ex)
      End Try

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 2011-04-23
    • 2018-02-27
    • 2023-03-07
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多