Search code examples
ibm-mqqtphp-uft

Connecting to IBM MQ from UFT


I have been trying to read and write messages to MQ from UFT. I am using the dotnet factory instance. i have reached till a point where i am able to connect to MQ while i am facing a problem in accessing the queue and read and write messages.

The code is as follows.

strQMgrName = "queue manager name"
strMQMDllPath = "C:\\Program Files (x86)\\IBM\WebSphere MQ\\bin\\amqmdnet.dll"
Set oMqEnvironment = DotNetFactory.CreateInstance("IBM.WMQ.MQEnvironment",strMQMDllPath)
oMqEnvironment.Hostname = "host name"
oMqEnvironment.Port = "port number"
oMqEnvironment.Channel = "channel name"


Set oMQC = DotNetFactory.CreateInstance("IBM.WMQ.MQC",strMQMDllPath)
' qmanager name,channel name, connection name
Set oMqQMgr = DotNetFactory.CreateInstance("IBM.WMQ.MQQueueManager",strMQMDllPath,strQMgrName)

oMqQMgr.isConnected ' gives true

Now i wan to use the method

public MQQueue AccessQueue(string queueName, int openOptions)

of the instance IBM.WMQ.MQQueueManager. can someone guide me in doing the same and let me know how i can push messages and read messages from the mentioned queue

Thank you


Solution

  • After a lot of googling and reading IBM documentation , i was able to put and get messages in MQ from UFT. .. Below in the code snippet that worked for me .. Hope this helps some one .. I was working on PUT and GET separately, hence there might be some repetition in the code.

    Prerequisite for this.. You have to download MQ Client from IBM site and install it on PC where UFT is installed.. IBM MQ client is a freeware

      Dim oMQEnvironment
        Dim oMQM
        Dim oMQC
        Dim oMQMessage
        Dim oMQQueue
        Dim intOpenOptions
    
    
        strMQMDLLPath = "C:\\Program Files (x86)\\IBM\WebSphere MQ\\bin\\amqmdnet.dll"
        strHostName= "Host Name"
        intPort = "Port Number"
        strChannel ="Channel Name"
        strMessage ="UFT Test Message"
        strMQManager = "Quemanager Name"
        strMessageQueue = "Queue Name"
    

    ' This part of the code is blatantly copied from one of the posts online. Will post the link once i find it again

    'for application(UFT) to connect to a queue manager in client mode

     Set oMQEnvironment = DotNetFactory.CreateInstance("IBM.WMQ.MQEnvironment",strMQMDLLPath)
    
        'Initize the Environment
        With oMQEnvironment
        .HostName = strHostName 
        .Port = intPort
        .Channel = strChannel 
        End with
    
        On Error Resume Next
        'Create MQ Instatnces
        Set oMQM = DotnetFactory.CreateInstance("IBM.WMQ.MQQueueManager",strMQMDLLPath)
    
        'Check if MQM Connected
        If Err.Number <> 0 Then
            Reporter.ReportEvent micFail , "Step: Creating MQM Object" , "Unable to Connect to MQ Manager at" & strHostName
            'Exit Test
        End If
    
        Set oMQC = DotnetFactory.CreateInstance("IBM.WMQ.MQC",strMQMDLLPath)
        Set oMQMessage = DotnetFactory.CreateInstance("IBM.WMQ.MQMessage",strMQMDLLPath)
    
        'Declare Q open options
        intOpenOptions = oMQC.MQOO_OUTPUT or oMQC.MQOO_FAIL_IF_QUIESCING ' 16 + 8192
    
        'Open the Q to post the messages
        If strRemoteMQManager = "" Then
        Set oMQQueue = oMQM.AccessQueue(strMessageQueue , intOpenOptions)
        Else 
        Set oMQQueue = oMQM.AccessQueue(strMessageQueue , intOpenOptions ,strRemoteMQManager, "","" )
        End If
    
        'Format Message
        With oMQMessage
        .CharacterSet = 819
        .WriteString(strMessage)
        End with
    
        'Post Message
        With oMQQueue
        .Put(oMQMessage)
        .Close()
        End With 
    

    Now for getting the message from MQ

    Dim oMQEnvironment
    Dim oMQM
    Dim oMQC
    Dim oMQMessage
    Dim oMQQueue
    
    strMQMDLLPath = "C:\\Program Files (x86)\\IBM\WebSphere MQ\\bin\\amqmdnet.dll"
    strHostName= "host name"
    intPort = "port number"
    strChannel ="channel name"
    strMessage ="UFT Test Message"
    strMessageQueue = "message queue intended to access"
    strMQManager = "mq manager name"
    strRemoteMQManager=""
    
    'Create MQ Instances
    Set oMQC = DotnetFactory.CreateInstance("IBM.WMQ.MQC",strMQMDLLPath)
    
    'set the properties of the Queue manager
    Set properties = DotNetFactory.CreateInstance("System.Collections.Hashtable")
                    properties.Add oMQC.HOST_NAME_PROPERTY, strHostName
                    properties.Add oMQC.PORT_PROPERTY, intPort
                    properties.Add oMQC.CHANNEL_PROPERTY, strChannel
    
    'access the queue manager 
    Set oMQM = DotnetFactory.CreateInstance("IBM.WMQ.MQQueueManager",strMQMDLLPath,strMQManager,properties)
    
    'here We are trying to browse the message one by one and keep the messages on the queue.
    
    'Declare Q open options
    Set oMQQueue = oMQM.AccessQueue(strMessageQueue,oMQC.MQOO_BROWSE)
    Set oMQGetMessageOptions = DotNetFactory.CreateInstance("IBM.WMQ.MQGetMessageOptions",strMQMDLLPath) 
    oMQGetMessageOptions.Options = oMQC.MQGMO_BROWSE_FIRST
    
    Set oMQMessage = DotnetFactory.CreateInstance("IBM.WMQ.MQMessage",strMQMDLLPath)
    
        oMQQueue.Get oMQMessage,oMQGetMessageOptions
    
    Set mqGetNextMsgOpts = DotNetFactory.CreateInstance("IBM.WMQ.MQGetMessageOptions",strMQMDLLPath) 
    mqGetNextMsgOpts.Options = oMQC.MQGMO_BROWSE_NEXT
    
    browseMessages = true
    
    Do while browseMessages
            on error resume next
            messageText = oMQMessage.ReadString(oMQMessage.MessageLength)
            'Print messageText
            Set oMQMessage = DotnetFactory.CreateInstance("IBM.WMQ.MQMessage",strMQMDLLPath)
            oMQQueue.Get oMQMessage,mqGetNextMsgOpts
            if Err.Number <> 0 then browseMessages =false   
            'Clear both MsgID and CorrelID for next use.
            oMQMessage.MessageId = oMQC.MQMI_NONE
            oMQMessage.CorrelationId = oMQC.MQCI_NONE           
    Loop 
    
    'Cleanup
    Set oMQQueue = Nothing 
    Set oMQMessage= Nothing 
    Set oMQOpenOptions= Nothing 
    Set oMQM= Nothing 
    Set oMQEnvironment = Nothing