Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C#

How to Setup a Websphere MQ with C# .NET: GUI to Put/Get to Local & Remote Queues

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
2 Jul 2009CPOL 98.7K   4.5K   16   13
Example to Put/Get Messages to Local and Remote Queues for Websphere beginners
Image 1

Introduction

This very basic article will show you how to write simple MQ programs in C# .NET as well as how to configure the background IBM Websphere Queue Manager.

Background

This example is more directed towards how to setup the Websphere MQ for communication to/from Local & Remote Queue Managers and how to create a C# .NET GUI. I've included a setup guide for the Queue Manager within the zipped source.

The Websphere DLL is also included within the source code.

Using the Code

Create a Windows Application copying the code found in Form.cs. Create the form as shown in the screenshot. The only label not visible is one called lblConnect to hold the results of hitting the Connect button.

The MQTest.cs can be taken and used anywhere communication with a Queue Manager is required.

C#
/// MQTest.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBM.WMQ;
namespace MQExample
{
/// <summary>
/// Author: Reza Servia
/// Name: MQTest.cs
/// Description:
/// MQ Manager Class
/// Put/Get Messages To/From Local Queue 
/// </summary>
public class MQTest
{
MQQueueManager queueManager;
MQQueue queue;
MQMessage queueMessage;
MQPutMessageOptions queuePutMessageOptions;
MQGetMessageOptions queueGetMessageOptions;
static string SendQueueName;
static string ReceiveQueueName;
static string QueueManagerName;
static string ChannelInfo;
string channelName;
string transportType;
string connectionName;
string message;
public MQTest()
{ 
//Initialization
/*
QueueManagerName = "QM_TEST";
QueueName = "QM_TEST.LOCAL.ONE";
ChannelInfo = "SVRCONN/TCP/localhost(1424)";
*/
}
/// <summary>
/// Connect to MQ Server
/// </summary>
/// <param name="strQueueManagerName">Queue Manager Name</param>
/// <param name="strChannelInfo">Channel Information</param>
/// <returns></returns>
public string ConnectMQ(string strQueueManagerName, string strChannelInfo)
{
QueueManagerName = strQueueManagerName;
ChannelInfo = strChannelInfo;
char[] separator = { '/' };
string[] ChannelParams;
ChannelParams = ChannelInfo.Split(separator);
channelName = ChannelParams[0];
transportType = ChannelParams[1];
connectionName = ChannelParams[2];
string strReturn = "";
try
{
queueManager = new MQQueueManager(QueueManagerName,
channelName, connectionName);
strReturn = "Connected Successfully";
}
catch (MQException exp)
{
strReturn = "Exception: " + exp.Message;
}
catch (Exception exp)
{
strReturn = "Exception: " + exp.Message;
}
return strReturn;
}
/// <summary>
/// Write Message to Local Queue
/// </summary>
/// <param name="strInputMsg">Text Message</param>
/// <param name="strqueueName">Queue Name</param>
/// <returns></returns>
public string WriteLocalQMsg(string strInputMsg, string strQueueName)
{
string strReturn = "";
SendQueueName = strQueueName;
try
{
queue = queueManager.AccessQueue(SendQueueName,
MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
message = strInputMsg;
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queuePutMessageOptions = new MQPutMessageOptions();
queue.Put(queueMessage, queuePutMessageOptions);
strReturn = "Message sent to the queue successfully";
}
catch (MQException MQexp)
{
strReturn = "Exception: " + MQexp.Message;
}
catch (Exception exp)
{
strReturn = "Exception: " + exp.Message;
}
return strReturn;
}
/// <summary>
/// Write Message to Local Queue
/// </summary>
/// <param name="strInputMsg">Text Message</param>
/// <returns></returns>
public string WriteLocalQMsg(string strInputMsg)
{
string strReturn = "";
try
{
queue = queueManager.AccessQueue(SendQueueName,
MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING );
message = strInputMsg;
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queuePutMessageOptions = new MQPutMessageOptions();
queue.Put( queueMessage, queuePutMessageOptions);
strReturn = "Message sent to the queue successfully";
}
catch (MQException MQexp)
{
strReturn = "Exception: "+ MQexp.Message;
}
catch (Exception exp)
{
strReturn = "Exception: "+ exp.Message;
}
return strReturn;
}
/// <summary>
/// Read Message from Local Queue
/// </summary>
/// <param name="strqueueName">Queue Name</param>
/// <returns>Text Message</returns>
public string ReadLocalQMsg(string strQueueName)
{
string strReturn = "";
ReceiveQueueName = strQueueName;
try
{
queue = queueManager.AccessQueue(ReceiveQueueName,
MQC.MQOO_INPUT_AS_Q_DEF + MQC.MQOO_FAIL_IF_QUIESCING);
queueMessage = new MQMessage();
queueMessage.Format = MQC.MQFMT_STRING;
queueGetMessageOptions = new MQGetMessageOptions();
queue.Get(queueMessage, queueGetMessageOptions);
strReturn = queueMessage.ReadString(queueMessage.MessageLength);
}
catch (MQException MQexp)
{
strReturn = "Exception: " + MQexp.Message;
}
catch (Exception exp)
{
strReturn = "Exception: " + exp.Message;
}
return strReturn;
}
}
}

History

  • 2nd July, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Indonesia Indonesia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to specify userid and password when connecting Pin
Member 225310017-Mar-22 3:00
Member 225310017-Mar-22 3:00 
QuestionPossible to get queue names dynamically? Pin
exdanl_Ikano4-May-16 5:19
exdanl_Ikano4-May-16 5:19 
QuestionAn attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) Pin
nivas.malli5-Oct-14 23:45
nivas.malli5-Oct-14 23:45 
GeneralMy vote of 4 Pin
Pab647-Nov-12 1:49
Pab647-Nov-12 1:49 
Questionhow to write a trigger or continuous listener for an IBM queue [modified] Pin
Member 297937818-Jul-11 21:23
Member 297937818-Jul-11 21:23 
GeneralDangerous error Pin
QuaKx28-Feb-11 22:46
QuaKx28-Feb-11 22:46 
QuestionHow to check the queue depth of remote MQ queue Pin
ak_netchat24-Feb-10 17:11
ak_netchat24-Feb-10 17:11 
AnswerRe: How to check the queue depth of remote MQ queue Pin
Reza Servia24-Feb-10 17:23
Reza Servia24-Feb-10 17:23 
GeneralRe: How to check the queue depth of remote MQ queue Pin
ak_netchat24-Feb-10 19:38
ak_netchat24-Feb-10 19:38 
QuestionProblem with MQ Series Pin
gonças12-Nov-09 3:55
gonças12-Nov-09 3:55 
AnswerRe: Problem with MQ Series [modified] Pin
Reza Servia8-Nov-09 21:07
Reza Servia8-Nov-09 21:07 
GeneralThanks a lot Pin
Boutemine Oualid24-Aug-09 4:30
Boutemine Oualid24-Aug-09 4:30 
GeneralRe: Thanks a lot Pin
Reza Servia8-Oct-09 17:41
Reza Servia8-Oct-09 17:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.