CodeProject
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); } } }
Thank you for the article its really helpful, would you please share the way to read the inserted message .
Have you done any research on this Khalil? If so, what have you found? I normally don’t provide answers to requests unless I see “effort” by the person asking… It shows me that you are sincere.
Do I require to install IBM Client Library
Add Reference”, then browse to “C:\Program Files (x86)\IBM\WebSphere MQ\bin” and add “amqmdnet.dll”
Yes, you’ll need to install the IBM websphere client. You can search for it with Google, and should be able to get it for free. There are different version of MQ, so I don’t know what you need.
Hi Alvin,
I have been trying to re-create your code above. I have downloaded and installed MQ Client and added the reference to the amqmdnet.dll to my visual studio project. However, when I try and run the project I get the following error
“The type or namespace name ‘IBM’ could not be found (are you missing a using directive or an assembly reference?)”
Do you have any suggestions please? I have been struggling with this for a while now.
Thanks very much.
Adam, have you tried commenting out the line:
`using IBM.WMQ;`???