Leer y Escribir colas MQ Series en C#

Lo primero es tener instalado el cliente de MQSeries… Una vez instalado la siguiente clase permite conectar, escribir y leer de una Cola MQ

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBM.WMQ;
using ClienteMQ_Core.Util;
namespace ClienteMQ_Core.Logic
{
/// <summary>
/// Clase que contiene la logica de conexion y
/// ===============================================================
/// </summary>
public class ConectorMQ
{
MQQueueManager queueManager;
MQQueue queue;
MQMessage queueMessage;
MQPutMessageOptions queuePutMessageOptions;
MQGetMessageOptions queueGetMessageOptions;
static string QueueName;
static string QueueManagerName;
static string ChannelInfo;
string channelName;
string transportType;
string connectionName;
string message;
/// <summary>
/// Conecta a MQ SERIES
/// ===============================================================================
/// </summary>
/// <param name=”strQueueManagerName”></param>
/// <param name=”strQueueName”></param>
/// <param name=”strChannelInfo”></param>
/// <returns></returns>
public string ConnectMQ(string strQueueManagerName, string strQueueName,string strChannelInfo)
{
QueueManagerName = strQueueManagerName;
QueueName = strQueueName;
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)
{
throw new ClienteMQException(exp);
}
return strReturn;
}
/// <summary>
/// Escribe un mensaje en la cola MQ conectada
/// ===============================================================================
/// </summary>
/// <param name=”strInputMsg”></param>
public void WriteMsg(string strInputMsg)
{
try
{
queue = queueManager.AccessQueue(QueueName,
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);
}
catch (MQException MQexp)
{
throw new ClienteMQException(MQexp);
}
catch (Exception exp)
{
throw new ClienteMQException(exp);
}
}
/// <summary>
/// Lee un mensaje de la cola seleccionada
/// ===============================================================================
/// </summary>
/// <returns></returns>
public string ReadMsg()
{
String strReturn = “”;
try
{
queue = queueManager.AccessQueue(QueueName,
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)
{
throw new ClienteMQException(MQexp);
}
catch (Exception exp)
{
throw new ClienteMQException(exp);
}
return strReturn;
}
}
}

Suerte 🙂