IBM WebSphere mq c # tool class and source code (net ),
A Brief Introduction to common objects of MQ
Queue Manager |
Queue Manager |
It is mainly responsible for Queue management and channel management. Similar to Oracle instances in Oracle, multiple Queue managers can be defined in one server. |
Queue |
Queue |
Is the object that stores the Message, which is the main operation object. Similar to the table concept in Oracle. |
Message |
Message |
A message actually placed in the Queue. The message can store strings, streams, and other objects. |
Channel |
Channel |
Is the link object of our application and two Queue Manager operations |
Process |
Process |
|
Classification of Queue:
Local Queue |
Local queue |
The object that stores messages. |
Remote Queue |
Remote Queue |
Queue for communication with another Queue Manager |
Mode Queue |
Template queue |
Create a queue template through which dynamic Queues with the same attributes as the template can be created. |
Dynamic Queue |
Dynamic (temporary queue) |
Temporary queue that can be created and deleted in the program |
MQ tool class:
using IBM.WMQ;
using IBM.WMQ.PCF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
// using IBM.WMQAX;
/// <summary>
/// MQHelp class library
/// henrylee
/// </ summary>
public class MQHelp
{
public MQHelp ()
{
}
/// <summary>
/// Create a local queue
/// </ summary>
/// <param name = "qmName"> Queue manager </ param>
/// <param name = "queueName"> queue name </ param>
/// <param name = "isTransmissionQueue"> is the transmission queue </ param>
public void CreateQueue (string qmName, string queueName, bool isTransmissionQueue = false)
{
PCFMessageAgent agent = new PCFMessageAgent (qmName);
PCFMessage request = new PCFMessage (CMQCFC.MQCMD_CREATE_Q);
request.AddParameter (MQC.MQCA_Q_NAME, queueName);
request.AddParameter (MQC.MQIA_Q_TYPE, MQC.MQQT_LOCAL);
if (isTransmissionQueue) request.AddParameter (MQC.MQIA_USAGE, MQC.MQUS_TRANSMISSION);
PCFMessage [] response = agent.Send (request);
}
/// <summary>
/// delete queue
/// </ summary>
public void DelQueue (string qmName, string queueName)
{
PCFMessageAgent agent = new PCFMessageAgent (qmName);
PCFMessage request = new PCFMessage (CMQCFC.MQCMD_DELETE_Q);
request.AddParameter (MQC.MQCA_Q_NAME, queueName);
PCFMessage [] response = agent.Send (request);
}
/// <summary>
/// read the message
/// </ summary>
/// <returns> </ returns>
public string ReadMessage (string qmName, string queueName)
{
MQQueueManager qMgr = new MQQueueManager (qmName);
MQQueue queue = qMgr.AccessQueue (queueName, MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING);
MQGetMessageOptions gmo = new MQGetMessageOptions ();
gmo.Options = MQC.MQGMO_WAIT;
gmo.WaitInterval = 1000;
gmo.MatchOptions = MQC.MQMO_NONE;
MQMessage message = new MQMessage ();
queue.Get (message);
return message.ReadString (message.MessageLength);
}
/// <summary>
/// write message
/// </ summary>
public void WriteMessage (string qmName, string queueName, string body)
{
MQQueueManager qMgr = new MQQueueManager (qmName);
MQQueue queue = qMgr.AccessQueue (queueName, MQC.MQOO_OUTPUT);
MQMessage message = new MQMessage ();
message.WriteString (body);
message.Format = MQC.MQFMT_STRING;
queue.Put (message);
}
/// <summary>
/// Get all queues of the current manager
/// </ summary>
/// <param name = "qmName"> Queue manager </ param>
/// <param name = "isFindSystemQueue"> whether the system queue is included </ param>
/// <returns> </ returns>
public List <string> GetALLQueue (string qmName, bool isFindSystemQueue = false)
{
PCFMessageAgent agent = new PCFMessageAgent (qmName);
PCFMessage request = new PCFMessage (CMQCFC.MQCMD_INQUIRE_Q_NAMES);
request.AddParameter (MQC.MQCA_Q_NAME, "*");
PCFMessage [] response = agent.Send (request);
string [] names = response [0] .GetStringListParameterValue (CMQCFC.MQCACF_Q_NAMES);
List <string> result = null;
if (! isFindSystemQueue)
result = names.ToList (). Where (s =>! s.Contains ("AMQ.") &&! s.Contains ("SYSTEM.")). ToList ();
else
result = names.ToList ();
return result;
}
/// <summary>
/// create remote queue
/// </ summary>
/// <param name = "qmName"> Queue manager </ param>
/// <param name = "queueName"> queue name </ param>
/// <param name = "ycqmName"> Remote queue manager name </ param>
/// <param name = "ycqueueName"> Remote queue name </ param>
/// <param name = "csqueueName"> Transmission queue name </ param>
public void CreateRemoteQueue (string qmName, string queueName, string ycqmName, string ycqueueName, string csqueueName)
{
PCFMessageAgent agent = new PCFMessageAgent (qmName);
PCFMessage request = new PCFMessage (CMQCFC.MQCMD_CREATE_Q);
request.AddParameter (MQC.MQCA_Q_NAME, queueName);
request.AddParameter (MQC.MQIA_Q_TYPE, MQC.MQQT_REMOTE);
request.AddParameter (MQC.MQCA_REMOTE_Q_MGR_NAME, ycqmName);
request.AddParameter (MQC.MQCA_REMOTE_Q_NAME, ycqueueName);
request.AddParameter (MQC.MQCA_XMIT_Q_NAME, csqueueName);
PCFMessage [] response = agent.Send (request);
agent.Disconnect ();
}
/// <summary>
/// Create sender channel
/// </ summary>
/// <param name = "qmName"> Queue manager </ param>
/// <param name = "channelName"> channel name </ param>
/// <param name = "transmissionQueueName"> transmission queue name </ param>
/// <param name = "iPPort"> IP address and port number Example: localhost (1415) </ param>
public void CreateChannelBySend (string qmName, string channelName, string transmissionQueueName, string iPPort)
{
PCFMessageAgent agent = new PCFMessageAgent (qmName);
PCFMessage request = new PCFMessage (CMQCFC.MQCMD_CREATE_CHANNEL);
request.AddParameter (CMQCFC.MQCACH_CHANNEL_NAME, channelName);
request.AddParameter (CMQCFC.MQIACH_CHANNEL_TYPE, MQC.MQCHT_SENDER);
request.AddParameter (CMQCFC.MQCACH_CONNECTION_NAME, iPPort);
request.AddParameter (CMQCFC.MQCACH_XMIT_Q_NAME, transmissionQueueName);
PCFMessage [] response = agent.Send (request);
agent.Disconnect ();
}
/// <summary>
/// Create receiver channel
/// </ summary>
/// <param name = "qmName"> Queue manager </ param>
/// <param name = "channelName"> channel name </ param>
public void CreateChannelByReceive (string qmName, string channelName)
{
PCFMessageAgent agent = new PCFMessageAgent (qmName);
PCFMessage request = new PCFMessage (CMQCFC.MQCMD_CREATE_CHANNEL);
request.AddParameter (CMQCFC.MQCACH_CHANNEL_NAME, channelName);
request.AddParameter (CMQCFC.MQIACH_CHANNEL_TYPE, MQC.MQCHT_RECEIVER);
PCFMessage [] response = agent.Send (request);
agent.Disconnect ();
}
}
The following is a front-end call:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default: System.Web.UI.Page
{
public const string MQNAME1 = "ManageTest1";
public const string MQNAME2 = "ManageTest2";
public readonly MQHelp MQHELP = new MQHelp ();
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
List <string> list = new MQHelp (). GetALLQueue (MQNAME1);
list.ForEach (s => ddlQueueList.Items.Add (new ListItem (s)));
}
}
protected void Button1_Click (object sender, EventArgs e)
{
Button but = sender as Button;
switch (but.Text)
{
case "Add queue":
MQHELP.CreateQueue (MQNAME1, txtQueue.Text.Trim ());
break;
case "Delete queue":
MQHELP.DelQueue (MQNAME1, txtQueue.Text.Trim ());
break;
case "Add message":
MQHELP.WriteMessage (MQNAME1, ddlQueueList.SelectedValue, txtMessage.Text.Trim ());
break;
case "Get message":
Literal1.Text = MQHELP.ReadMessage (MQNAME1, ddlQueueList.SelectedValue);
break;
case "Add transmission queue":
MQHELP.CreateQueue (txtTMName.Text, txtTName.Text.Trim (), true);
break;
case "Delete transmission queue":
MQHELP.DelQueue (txtTMName.Text, txtTName.Text.Trim ());
break;
case "Add remote queue":
MQHELP.CreateRemoteQueue (txtRMName.Text, txtRQName.Text, txtRMTName.Text, txtRMTQName.Text, txtRTQName.Text);
break;
case "Add sender channel":
MQHELP.CreateChannelBySend (txtCMName.Text, txtCCName.Text, txtCTName.Text, txtCip.Text);
break;
case "Add receiver channel":
MQHELP.CreateChannelByReceive (txtCMName.Text, txtCCName.Text);
break;
default:
break;
}
}
}