你的位置:首页 > ASP.net教程

[ASP.net教程]IBM WebSphere MQ的C#工具类以及源码(net)


简单的介绍一下MQ常用的对象

 

Queue Manager

队列管理器

主要负责管理队列、通道等,类似与Oracle中的Oracle实例的概念,在一台服务器中可以定义多个Queue Manager。

Queue

队列

是存放Message的对象,是我们主要操作的对象。类似与Oracle中的表的概念。

Message

消息

实际放入Queue中的消息,该消息可以存储字符串、Stream等对象。

Channel

通道

是我们的应用以及两个Queue Manager操作的链接对象

Process

过程

 

 

Queue的分类:

Local Queue

本地队列

主要存储消息的对象

Remote Queue

远程队列

和另外一个Queue Manager通讯的队列

Mode Queue

模板队列

建立一个队列模板,通过这个模板可以建立与模板相同属性的动态队列。

Dynamic Queue

动态对了(临时队列)

可以在程序中创建和删除的临时队列

 

MQ工具类:

using IBM.WMQ;using IBM.WMQ.PCF;using System;using System.Collections.Generic;using System.Linq;using System.Web;//using IBM.WMQAX;/// <summary>/// MQHelp 类库///  henrylee/// </summary>public class MQHelp{  public MQHelp()  {  }  /// <summary>  /// 创建本地队列  /// </summary>  /// <param name="qmName">队列管理器</param>  /// <param name="queueName">队列名称</param>  /// <param name="isTransmissionQueue">是否为传输队列</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>  /// 删除队列  /// </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>  /// 读取消息  /// </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>  /// 写入消息  /// </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>  /// 获取当前管理器所有队列  /// </summary>  /// <param name="qmName">队列管理器</param>  /// <param name="isFindSystemQueue">是否包含系统队列</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>  /// 创建远程队列  /// </summary>  /// <param name="qmName">队列管理器</param>  /// <param name="queueName">队列名称</param>  /// <param name="ycqmName">远程队列管理器名称</param>  /// <param name="ycqueueName">远程队列名称</param>  /// <param name="csqueueName">传输队列名称</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>  /// 创建发送方通道  /// </summary>  /// <param name="qmName">队列管理器</param>  /// <param name="channelName">通道名称</param>  /// <param name="transmissionQueueName">传输队列名称</param>  /// <param name="iPPort">IP地址与端口号 例: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>  /// 创建接受方通道  /// </summary>  /// <param name="qmName">队列管理器</param>  /// <param name="channelName">管道名称</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();  }}

以下是前台调用:

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 "添加队列":        MQHELP.CreateQueue(MQNAME1, txtQueue.Text.Trim());        break;      case "删除队列":        MQHELP.DelQueue(MQNAME1, txtQueue.Text.Trim());        break;      case "添加消息":        MQHELP.WriteMessage(MQNAME1, ddlQueueList.SelectedValue, txtMessage.Text.Trim());        break;      case "获取消息":        Literal1.Text = MQHELP.ReadMessage(MQNAME1, ddlQueueList.SelectedValue);        break;      case "添加传输队列":        MQHELP.CreateQueue(txtTMName.Text, txtTName.Text.Trim(), true);        break;      case "删除传输队列":        MQHELP.DelQueue(txtTMName.Text, txtTName.Text.Trim());        break;      case "添加远程队列":        MQHELP.CreateRemoteQueue(txtRMName.Text, txtRQName.Text, txtRMTName.Text, txtRMTQName.Text, txtRTQName.Text);        break;      case "添加发送方通道":        MQHELP.CreateChannelBySend(txtCMName.Text, txtCCName.Text, txtCTName.Text, txtCip.Text);        break;      case "添加接收方通道":        MQHELP.CreateChannelByReceive(txtCMName.Text, txtCCName.Text);        break;      default:        break;    }  }}