[VB.NET] Message Queue 接收與發送

  • 426
  • 0

  使用 VB.NET 操作 Microsoft Message Queue (MSMQ) 訊息收發。

引用

  引用 System.Messaging 操作 Microsoft Message Queue 服務。

Import System.Messaging

   定義兩個 Function,分別處理 Message Queue 接收與發送。

    Public Sub Main()

        Dim str_MessageQueuePath As String

        str_MessageQueuePath = "FORMATNAME:DIRECT=OS:.\private$\myqueue"

        ' Initial MQ receive handler
        InitailMQ(str_MessageQueuePath)

        ' Send message to MQ
        SendMessage(str_MessageQueuePath, "Test Message", "Label")

    End Sub

接收訊息

  在 InitialMQ 中 AddHandler 處理MessageQueue.ReceiveCompleted事件,添加 Handler 之後呼叫MessageQueue.BeginReceive(),Handler 即開始接收 Message Queue 訊息。

  當 Handler 看到 MQ 中有新訊息時會觸發 MessageQueue.ReceiveCompleted事件,在事件最後再次呼叫MessageQueue.BeginReceive()以觸發 Handler 再次開始接收訊息。


    Public Sub InitailMQ(ByVal str_MessageQueuePath As String)


        Dim obj_MessageQueue As MessageQueue
        Dim obj_XmlFormatter As XmlMessageFormatter

        Try
        
            obj_MessageQueue = New MessageQueue(str_MessageQueuePath)
            obj_XmlFormatter = CType(obj_MessageQueue.Formatter, XmlMessageFormatter)
            obj_XmlFormatter.TargetTypeNames = New String() {"System.String,mscorlib"}
            AddHandler obj_MessageQueue.ReceiveCompleted, AddressOf OnReceiveMessage
            obj_MessageQueue.BeginReceive()
            Console.WriteLine("Initial MQ successful")
            
        Catch ex As Messaging.MessageQueueException
        
            Console.WriteLine(ex.ToString)
            
        End Try

    End Sub

    Public Sub OnReceiveMessage(ByVal source As Object, ByVal asyncResult As ReceiveCompletedEventArgs)

        Dim obj_MessageQueue As MessageQueue
        Dim obj_Message As Message
        
        obj_MessageQueue = CType(source, MessageQueue)
        obj_Message = obj_MessageQueue.EndReceive(asyncResult.AsyncResult)

        Try

            ' Print MQ message label
            Console.WriteLine("Message Label: " & obj_Message.Label)

            ' Print MQ message body
            Console.WriteLine("Message Body: " & obj_Message.Body.ToString)

        Catch ex As Exception
        
            Console.WriteLine(ex.ToString)
            
        Finally
        
            obj_MessageQueue.BeginReceive()
            
        End Try

    End Sub

發送訊息

  宣告 Message 物件並定義Message.BodyMessage.Label,再以MessageQueue.Send(Message)發送訊息。


    Public Sub SendMessage(ByVal str_MessageQueuePath As String, ByVal str_MessageBody As String,
                           ByVal str_MessageLabel As String)

        Dim obj_MessageQueue As New MessageQueue(str_MessageQueuePath)
        Dim obj_Message As New Message()

        Try
        
            obj_Message.Formatter = New ActiveXMessageFormatter
            obj_Message.Body = str_MessageBody
            obj_Message.Label = str_MessageLabel
            obj_MessageQueue.Send(obj_Message)
            obj_MessageQueue = Nothing
            obj_Message = Nothing
            
        Catch ex As Messaging.MessageQueueException
        
            Console.WriteLine(ex.ToString)
            
        End Try

    End Sub