/** * 进行MQ连接初始化 * @return TRUE:连接成功;FALSE:连接失败 */ public Boolean initMQConnection() { Boolean ret = false; currentIndex = -1; while (doNextCurrMQC()) { try { if (cfgSendData != null) { // 接收队列连接参数 ret = MQQueueAccesser.connectMQ(cfgSendData, MQQueueAccesser.MQ_MSGSEND_TYPE); } if (cfgReceiveData != null) { // 发送队列连接参数 ret = MQQueueAccesser.connectMQ(cfgReceiveData, MQQueueAccesser.MQ_MSGRECEIVE_TYPE); } return(ret); } catch (Exception e) { LogUtil.Error(this, "MQ Client 初始化 Exception:" + e.Message); if (cfgSendData != null) { cfgSendData.release(); cfgSendData = null; } if (cfgReceiveData != null) { cfgReceiveData.release(); cfgReceiveData = null; } ret = false; } } return(ret); }
/** * 连接到MQ队列管理器 * @param para 连接对象 * @param type 连接类型(发送,接收) * @param trycount 重试连接次数 * @param interval 重连等待时间 * @return TRUE:连接成功;FALSE:连接失败; * @throws EisException 当重试连接次数都失败,则返回异常 */ public static Boolean connectMQ(MQCParameter para, int type, int trycount, int interval) { LogUtil.Info("try to connect mq :", para.mqParameter.toString()); Boolean ret = false; MQParameter mqParameter = para.mqParameter; Hashtable props = new Hashtable(); if (mqParameter.getHostName() != null) { props.Add(MQC.HOST_NAME_PROPERTY, mqParameter.getHostName()); } if (mqParameter.getPort() != 0) { props.Add(MQC.PORT_PROPERTY, mqParameter.getPort()); } if (mqParameter.getChannel() != null) { props.Add(MQC.CHANNEL_PROPERTY, mqParameter.getChannel()); } if (mqParameter.getCcsid() != 0) { props.Add(MQC.CCSID_PROPERTY, mqParameter.getCcsid()); } // MQPoolToken token=MQEnvironment.addConnectionPoolToken(); int i = 0; MQQueue queue = null; MQQueueManager qManager = null; while (trycount <= 0 || (trycount > 0 && i < trycount)) { i++; try { para.release(); //连接到指定的队列管理器 qManager = new MQQueueManager(mqParameter.getQManagerName(), props); //根据参数不同连接到Q队列上 if (MQ_MSGRECEIVE_TYPE == type) { // queue = qManager.accessQueue(mqParameter.getQueueName(),MQC.MQOO_INPUT_AS_Q_DEF); //queue = qManager.AccessQueue(mqParameter.getQueueName(), MQC.MQOO_INQUIRE | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INPUT_SHARED); queue = qManager.AccessQueue(mqParameter.getQueueName(), MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE); } else if (MQ_MSGSEND_TYPE == type) { queue = qManager.AccessQueue(mqParameter.getQueueName(), MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_INQUIRE); // queue = qManager.AccessQueue(mqParameter.getQueueName(), MQC.MQOO_OUTPUT); } para.qManager = qManager; para.queue = queue; ret = true; break; } catch (MQException mqe) { LogUtil.Error("", mqe); if (i == trycount) { LogUtil.Error( "不能连接到MQ Server :", para.mqParameter.toString() + "]," + "已经做了[" + i + "]次尝试!"); throw mqe; } else { try { // 在下一次重试之前等待一定时间 Thread.Sleep(interval); } catch (Exception e) { LogUtil.Error("Exception:", e); throw new EisException("interrupted when connect sleeping"); } } } }// end of while loop props.Clear(); return(ret); }