How to "PUT" and "GET" queue item IBM WebSphere Queue - C#

This is example C# code how to insert value into IBM WebSphere  Queue, and Get The Queue value back.
You can use Queue to store some information to processed later without involve database. You can read more about IBM WebSphere queue in here

Put Message C# Code

            MQQueueManager queueManager;
            MQQueue queue;
            MQMessage queueMessage;
            MQPutMessageOptions queuePutMessageOptions;
            MQGetMessageOptions queueGetMessageOptions;


            string ChannelInfo;
            string channelName;
            string transportType;
            string connectionName;

            //get channel info
            char[] separator = { '/' };
            string[] ChannelParams;

            ChannelInfo = "CHANNEL3/TCP/172.19.37.2";
            ChannelParams = ChannelInfo.Split(separator);
            channelName = ChannelParams[0];
            transportType = ChannelParams[1];
            connectionName = ChannelParams[2];

            //get queue manager
            string queueManagerName = "QMGR3";
            string queueName = "TESTQ";

            queueManager = new MQQueueManager(queueManagerName, channelName, connectionName);
            queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
            queueMessage = new MQMessage();
            queueMessage.WriteString("TEST");
            queueMessage.Format = MQC.MQFMT_STRING;
            queuePutMessageOptions = new MQPutMessageOptions();

            //putting the message into the queue
            queue.Put(queueMessage, queuePutMessageOptions);
            queue.Close();
            MessageBox.Show("Successfully put data into queue");



Get Message C# Code

            MQQueueManager queueManager;
            MQQueue queue;
            MQMessage queueMessage;
            MQPutMessageOptions queuePutMessageOptions;
            MQGetMessageOptions queueGetMessageOptions;

            string ChannelInfo;
            string channelName;
            string transportType;
            string connectionName;

            //get channel info
            char[] separator = { '/' };
            string[] ChannelParams;
            ChannelInfo = "CHANNEL3/TCP/172.19.37.2";

            ChannelParams = ChannelInfo.Split(separator);
            channelName = ChannelParams[0];
            transportType = ChannelParams[1];
            connectionName = ChannelParams[2];

            //get queue manager
            string queueManagerName = "QMGR3";
            string queueName = "TESTQ";

            queueManager = new MQQueueManager(queueManagerName, channelName, connectionName);
            queue = queueManager.AccessQueue(queueName, MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
            string str_rtn = string.Empty;

            while (true)
            {
                try
                {
                    queueMessage = new MQMessage();
                    queueMessage.Format = MQC.MQFMT_STRING;

                    queueGetMessageOptions = new MQGetMessageOptions();
                    queue.Get(queueMessage, queueGetMessageOptions);
                    str_rtn = queueMessage.ReadString(queueMessage.MessageLength);

                    MessageBox.Show(str_rtn);
                    break;

                }
                catch (MQException MQExp)
                {
                    str_rtn = "MQQueue::Get ended with " + MQExp.Message;
                    MessageBox.Show(str_rtn);
                    return;                  

                }
            }
            queue.Close();


Note : You need to include library IBM.WMQ . ( using IBM.WMQ; )


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

How to create DataGrid or GridView in JSP - Servlet

Control Webpart Visible/Enable using macro in Kentico