Recently I had to figure out an easy way to send a message to an IBM WebSphere MQ queue. IBM provides many sample on how to get this done. I used the “nmqsput.cs” sample as a reference.

In order to create this I used Visual Studio 2012 and created a Console Application called “SendToWebsphere” and then once the project is open select “Project > Add Reference”, then browse to “C:\Program Files (x86)\IBM\WebSphere MQ\bin” and add “amqmdnet.dll” as a reference.

In my application I wanted to be able to pass command line parameters like so:

SendToWebsphere <queue> <file>

Where <queue> is the queue to send to, and <file> is a file that contains XML to send to the queue (this is a single line).

Here is the code I created:

</pre>
using System;
using IBM.WMQ;

namespace SendToWebSphere
{
    class sendToQueue
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main(String[] args)
        {
            MQQueueManager mqQMgr;          // MQQueueManager instance
            MQQueue mqQueue;         // MQQueue instance
            MQMessage mqMsg;           // MQMessage instance
            MQPutMessageOptions mqPutMsgOpts;    // MQPutMessageOptions instance
            String queueName;       // Name of queue to use

            if (args.Length != 2)
            {
                System.Console.WriteLine("Usage: SendToWebSphere <queue> <xml_Filename>");
                return ((int)-1);
            }
            else
            {
                queueName = args[0];
            }

            ///
            /// Try to create an MQQueueManager instance 
            /// 
            try
            {
                String QueueManager = "QM_DVW864_QA5_BT";
                String channelName = "S_DVW864_QA5";
                String connectionName = "DVW864-QA5(1419)";

                mqQMgr = new MQQueueManager(QueueManager, channelName, connectionName);
            }
            catch (MQException mqe)
            {
                // stop if failed
                System.Console.WriteLine("create of MQQueueManager ended with " + mqe.ToString());
                return ((int)mqe.Reason);
            }

            ///
            /// Try to open the queue
            ///
            try
            {
                mqQueue = mqQMgr.AccessQueue(queueName,
                                              MQC.MQOO_OUTPUT                   // open queue for output
                                              + MQC.MQOO_FAIL_IF_QUIESCING);   // but not if MQM stopping
            }
            catch (MQException mqe)
            {
                // stop if failed
                System.Console.WriteLine("MQQueueManager::AccessQueue ended with " + mqe.ToString());
                return ((int)mqe.Reason);
            }

            ///
            /// Read lines from the console and put them to the message queue
            /// Loop until end of input, or there is a failure
            /// 
            string[] lines = System.IO.File.ReadAllLines(@args[1]);
            string message = String.Join("\r\n", lines);
            
            // put the next message to the queue
            mqMsg = new MQMessage();
            mqMsg.CharacterSet = 1208;  // Set UTF-8 encoding
            mqMsg.WriteString(message);
            mqMsg.Format = MQC.MQFMT_STRING;
            mqPutMsgOpts = new MQPutMessageOptions();
            try
            {
                mqQueue.Put(mqMsg, mqPutMsgOpts);
            }
            catch (MQException mqe)
            {
                // report the error
                System.Console.WriteLine("MQQueue::Put ended with " + mqe.ToString());
            }

            try
            {
                //Close the Queue
                mqQueue.Close();

                //Close the Queue Manager
                mqQMgr.Disconnect();
            }
            catch (MQException mqe)
            {
                System.Console.WriteLine("A WebSphere MQ error occurred :Completion code " +
                    mqe.CompletionCode + "Reason code " + mqe.ReasonCode);
                return ((int)mqe.Reason);
            }

            System.Console.WriteLine("Sample NMQSPUT end");
            return (0);
        }
    }
}

Advertisement