Sending to a Remote Private Message Queue with the Script Task

New: 14 April 2006

Microsoft Windows Message Queuing makes it easy for developers to communicate with application programs quickly and reliably by sending and receiving messages. A message queue may be located on the local computer or a remote computer, and may be public or private. The Integration Services MSMQ Connection Manager and Message Queue Task do not support sending to a private queue on a remote computer. However, by using the Script task, it is easy to send a message to a remote private queue.

If you want to create a task that you can more easily reuse across multiple packages, consider using the code in this Script task sample as the starting point for a custom task. For more information, see Developing a Custom Task.

The following example uses an existing MSMQ connection manager, together with objects and methods from the System.Messaging namespace, to send the text contained in a package variable to a remote private message queue. The call to the M:Microsoft.SqlServer.Dts.ManagedConnections.MSMQConn.AcquireConnection(System.Object) method of the MSMQ connection manager returns a MessageQueue object whose Send method accomplishes this task.

  1. Create an MSMQ connection manager with the default name. Set the path of a valid remote private queue, in the following format:

  2. Create an Integration Services variable named MessageText of type String to pass the message text into the script. Enter a default message as the value of the variable.

  3. Add a Script Task to the design surface and edit it. On the Script tab of the Script Task Editor, add the MessageText variable to the ReadOnlyVariables property to make the variable available inside the script.

  4. Click Design Script to open the Visual Studio for Applications script editor.

  5. Add a reference in the script project to the System.Messaging namespace.

  6. Replace the contents of the script window with the code in the following section.

Imports System
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Messaging

Public Class ScriptMain

    Public Sub Main()

        Dim remotePrivateQueue As MessageQueue
        Dim messageText As String

        remotePrivateQueue = _
            DirectCast(Dts.Connections("Message Queue Connection Manager").AcquireConnection(Dts.Transaction), _
        messageText = DirectCast(Dts.Variables("MessageText").Value, String)

        Dts.TaskResult = Dts.Results.Success

    End Sub

End Class

Community Additions