Connecting to Remote IBM WebSphere MQ Server

The below post will provide a “hello world” kind of introduction to connecting to a remote IBM WebSphere Message Queue server using client application written in C#.

Points to note:

  • Server and client should have a common OS login name. The passwords can be different between the server and client, what matters is that the login name should be identical.
  • The login name you have selected to use should be part of the MQM group on the MQ server, or it should be explicitly given permission for querying the Queue manager and the queue.
  • I have come across code on the web where they have mentioned how to connect to MQ server even with a different OS login name from the client side by explicitly setting the login and password onto the MQEnviroment class static properties, but it has not worked for me till now. So, my example code would be assuming that there is an identical login name between the client and server.
  • Make a note of the Server-connection channel name (case sensitive), queue manager name (case sensitive), queue name (case sensitive) from the server side.
  • You would need to install at the least the client package of MQ onto the client machine, the client code would be using the library from this installation to connect to the server.
  • A reference to amqmdnet.dll would have to be added to the client project, its located in <MQ Installation Path>binamqmdnet.dll

 

Now for the code

using System;
using System.Collections.Generic;
using System.Text;
using IBM.WMQ;
 
namespace RemoteMQTest {
    class Test01 {
        static void Main(string[] args) {
            MQEnvironment.Hostname = "My_Server";
            MQEnvironment.Channel = "My_Channel";
 
            string mqQueueManagerName = "My_Q_Manager";
            string mqQueueName = "My_Queue";
            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF;
 
            MQQueueManager qMgr = new MQQueueManager(mqQueueManagerName);
            MQQueue q = qMgr.AccessQueue(mqQueueName, openOptions);
 
            MQMessage message = new MQMessage();
            message.Version = 2;
 
            q.Get(message);
 
            Console.WriteLine("I got the message : {0}", message.ReadString(message.DataLength));
            message.Seek(0);
 
 
            q.Close();
            qMgr.Close();
            Console.WriteLine("done!");
        }
    }
}
  

Note that in line 25, we are explicitly making the message to seek back to 0th byte.

 

Happy coding!

Leave a Reply