C#连接IBMMQ

正确安装要注意几个地方,集群,通道发送方和通道接收方。

    要想正确发送消息,必须具备以下条件:

    1、传输队列。(本地队列,用法选传输)

    2、消息通道(注意填写链接名称是对方IP,传输队列都填上)

    3、远程队列(注意填写远程队列和远程队列管理器名称)

    然后往远程队列里面填写数据,就会发送到远程的队列了!!

    C#连接IBMMQ需要引用以下几个DLL,该DLLIBMMQ安装目录bin文件夹下!amqmdnet.dllimqc23vn.dllmqic32.dllamqmdnet.dll

    代码如下:

    using System;

    using System.Collections.Generic;

    using System.Text;

   

using IBM.WMQ.PCF;

    using IBM.WMQ;

   

namespace test{

    internal class IBMMQClass

    {

    public void CreateQueueManage(string qmName)

    {

    throw new Exception("不会创建!");

    }

    /// <summary>

    /// 创建本地队列

    /// </summary>

    /// <param name="qmName">队列管理器名称</param>

    /// <param name="queueName">本地队列名称</param>

    public void CreateQueue(string qmName, string queueName)

    {

    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);

    PCFMessage[] response = agent.Send(request);

    agent.Disconnect();

    }

    /// <summary>

    /// 创建远程队列

    /// </summary>

    /// <param name="qmName">队列管理器</param>

    /// <param name="queueName">队列名称</param>

    /// <param name="ycqmName">远程队列管理器名称</param>

    /// <param name="ycqueueName">远程队列名称</param>

    /// <param name="csqueueName">传输队列名称</param>

    public void CreateYuanchengQueue(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="isSend">True为发送方,false为接收方</param>

    public void CreateChannel(bool isSend)

    {

    PCFMessageAgent agent = new PCFMessageAgent("QM_zhang");

    PCFMessage request = new PCFMessage(CMQCFC.MQCMD_CREATE_CHANNEL);

    request.AddParameter(CMQCFC.MQCACH_CHANNEL_NAME, "mama");

    if (!isSend)

    {

    request.AddParameter(CMQCFC.MQIACH_CHANNEL_TYPE, MQC.MQCHT_RECEIVER);

    }

    else

    {

    request.AddParameter(CMQCFC.MQIACH_CHANNEL_TYPE, MQC.MQCHT_SENDER);

    request.AddParameter(CMQCFC.MQCACH_CONNECTION_NAME, "192.168.3.232");

    request.AddParameter(CMQCFC.MQCACH_XMIT_Q_NAME, "bb");

    }

    PCFMessage[] response = agent.Send(request);

    agent.Disconnect();

    }

    /// <summary>

    /// 删除队列

    /// </summary>

    /// <param name="qmName">队列管理器名称</param>

    /// <param name="queueName">队列名称</param>

    public void DeleteQueue(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);

    agent.Disconnect();

    }

    /// <summary>

    /// 发送消息

    /// </summary>

    /// <param name="qmName">队列管理器</param>

    /// <param name="queueName">队列名称</param>

    /// <param name="body">消息内容</param>

    public void PutMessage(string qmName, string queueName, string body)

    {

    MQQueueManager qMgr = new MQQueueManager(qmName);

    MQQueue queue = qMgr.AccessQueue(queueName, MQC.MQOO_OUTPUT);

    try

    {

    MQMessage message = new MQMessage();

    message.WriteString(body);

    message.Format = MQC.MQFMT_STRING;

    queue.Put(message);

    }

    catch { }

    finally

    {

    queue.Close();

    }

    }

    /// <summary>

    /// 获取消息

    /// </summary>

    /// <param name="qmName">队列管理器名称</param>

    /// <param name="queueName">队列名称</param>

    /// <returns>消息内容</returns>

    public string GetMessage(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);

    try

    {

    MQMessage message = new MQMessage();

    queue.Get(message);

    return message.ReadString(message.MessageLength);

    }

    catch

    { }

    finally

    {

    queue.Close();

    }

    }

    /// <summary>

    /// 获取消息

    /// </summary>

    /// <param name="qmName">队列管理器名称</param>

    /// <param name="queueName">队列名称</param>

    /// <param name="timeInterval">等待时间(毫秒)</param>

    /// <param name="qMgr">队列管理器实体类</param>

    /// <returns>消息内容</returns>

    public string GetMessage(string qmName, string queueName, int timeInterval, out MQQueueManager qMgr)

    {

        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 = timeInterval;

    gmo.MatchOptions = MQC.MQMO_NONE;

    MQMessage message = new MQMessage();

    queue.Get(message, gmo);

    return message.ReadString(message.MessageLength);

    }

 }

    }

 

你可能感兴趣的:(IBM)