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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBM.WMQ;
namespace MQExample
{
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()
{
}
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;
}
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;
}
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;
}
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.