IBM MQ 安装连接所遇到的问题


安装问题

下载安装包:public.dhe.ibm.com/ibmdl/export/pub/software/websphere/messaging/mqadv/

需要选择定制类型安装 如果用典型安装会少一些dll  导致连接失败

连接问题

MQRC_NOT_AUTHORIZED

需要右键队列管理器 添加用户 mqm    取消权限 不然还是无效

在CMD中打开MQ队列管理器取消权限:

runmqsc 队列管理器名称

ALTER QMGR CHLAUTH(DISABLED)  

修改字符集编码

RUNMQSC 队列管理器名称

ALTER QMGR CCSID (819)


END

Dll 路径 

安装路径下IBM\MQ\Tools\Lib

示例路径:IBM\MQ\Tools\dotnet\samples\cs

连接代码示例

public class MQHandler : IDisposable
    {
        private static MQQueueManager QM = null;
        private string SendQueueName = string.Empty;
        private string ReceiveQueueName = string.Empty;

        private MQQueue getQueue=null;
        private MQGetMessageOptions mqGetMessageOptions;
        private bool islog = false;
        private bool _success = false;

        public bool Success { get { return _success; } }
        public MQHandler()
        {
            Init();
        }
        private void Init()
        {
            //队列ip
            MQEnvironment.Hostname = ConfigurationManager.AppSettings["Hostname"];
            //端口号
            MQEnvironment.Port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
            //队列管理器
            string qmName = ConfigurationManager.AppSettings["QueueManager"];
            //队列通道
            MQEnvironment.Channel = ConfigurationManager.AppSettings["Channel"];
            MQEnvironment.UserId = ConfigurationManager.AppSettings["Userid"];
            MQEnvironment.Password = ConfigurationManager.AppSettings["Password"];
            //Environment.SetEnvironmentVariable("MQCCSID", "1381");
            //队列名称
            SendQueueName = ConfigurationManager.AppSettings["SendQueueName"];
            ReceiveQueueName = ConfigurationManager.AppSettings["ReceiveQueueName"];

            islog = ConfigurationManager.AppSettings["IsLog"] == "yes" ? true : false;
            try
            {
                if (QM == null || !QM.IsConnected)
                {
                    QM = new MQQueueManager(qmName);
                    _success = true;
                    OracleLogWriter.WriteSystemLog("ibmmq连接成功", "INFO");
                }
            }
            catch (Exception e)
            {
                OracleLogWriter.WriteSystemLog($"启动ibm mq 异常:{e.Message},堆栈信息:{e.StackTrace}", "ERROR");
            }
        }
        /// 
        /// 发送消息
        /// 
        /// 
        public void Send(object val)
        {
            try
            {
                int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
                //使用默认的消息选项
                MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
                MQMessage mqMessage = new MQMessage();
                mqMessage.Write(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(val)));
                //创建队列
                MQQueue putQueue = QM.AccessQueue(SendQueueName, openOptions);
                putQueue.Put(mqMessage, mqPutMessageOptions);
                putQueue.Close();
            }
            catch (Exception exception)
            {
                OracleLogWriter.WriteSystemLog($"发送mq异常:{exception.Message},堆栈信息:{exception.StackTrace}", "ERROR");
            }
        }

        /// 
        /// 获取拉取队列实例
        /// 
        public void GetQueueInstance()
        {
            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INQUIRE;
            mqGetMessageOptions = new MQGetMessageOptions();
            mqGetMessageOptions.Options = MQC.MQGMO_WAIT;
            mqGetMessageOptions.WaitInterval = 5000;
            mqGetMessageOptions.MatchOptions = MQC.MQMO_NONE;
            getQueue = QM.AccessQueue(ReceiveQueueName, openOptions);//创建队列
        }
        /// 
        /// 接受消息
        /// 
        /// 
        public DZSDMessage Receive()
        {
            try
            {
                if (getQueue==null)
                {
                    GetQueueInstance();
                }
                int depth = getQueue.CurrentDepth;//获取队列深度 判断是否有数据
                if (depth == 0)
                    return null;
                MQMessage Message = new MQMessage();
                getQueue.Get(Message, mqGetMessageOptions);
                byte[] res = null;
                Message.ReadFully(ref res);
                DZSDMessage res2 = new DZSDMessage();
                res2.data = Encoding.UTF8.GetString(res);
                JObject obj = JObject.Parse(res2.data);
                res2.messageType = obj["messageType"].ToString();
                return res2;
                //return JsonConvert.DeserializeObject(res1);
            }
            catch (Exception e)
            {
                if (islog)
                {
                    OracleLogWriter.WriteSystemLog($"接收mq异常:{e.Message},堆栈信息:{e.StackTrace}", "ERROR");
                }

                return null;
            }
        }

        public void Dispose()
        {
            QM.Disconnect();
            getQueue.Close();
        }
    }


						  
					  
						
							
MQ