}// end of method /** * 将响应消息放入发送队列 * @param para 连接实例 * @param message 准备发送的消息 * @param type * @return * @throws EisException */ public static Boolean putMsgToQueue(MQCParameter para, MQMessage message) { MQPutMessageOptions pmo = getPMOFromPara(); if (null == message || null == pmo || null == para) { throw new EisException(MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_CODE); } try { if (para.qManager == null || para.queue == null) { connectMQExce(para, MQQueueAccesser.MQ_MSGSEND_TYPE); } printMsgId("before put MsgId", message.MessageId); LogUtil.Info("put message.replyToQueueManagerName:", message.ReplyToQueueManagerName); LogUtil.Info("put message.messageType:", message.MessageType); LogUtil.Info("put message.report:", message.Report); LogUtil.Info("message.encoding:", message.Encoding); LogUtil.Info("message.characterSet:", message.CharacterSet); LogUtil.Info("message.format:", message.Format); printMsgId("before put correlationId", message.CorrelationId); para.queue.Put(message, pmo); para.qManager.Commit(); printMsgId("after put MsgId", message.MessageId); return(true); } catch (MQException mqe) { throw mqe; } finally { pmo = null; } }
/** * 通过匹配的msgID来从Queue中获取消息 * @param msgId Q队列的消息标识 * @param para MQ连接实例对象 * @param type 设置接收消息 * @param timeout < 0:则无限等待;< 0:从MQ队列连接的超时时间,单位秒 * @return * @throws EisException */ public static MQMessage getMsgFromQueue(byte[] msgId, MQCParameter para, int timeout) { try{ MQGetMessageOptions gmo = getGMOFromPara(timeout); if (para.qManager == null || para.queue == null) { connectMQExce(para, MQQueueAccesser.MQ_MSGRECEIVE_TYPE); } MQMessage msg = new MQMessage(); if (msgId != null) { msg.MessageId = msgId; } printMsgId("before get MsgId", msg.MessageId); // 从MQ请求队列拿消息 para.queue.Get(msg, gmo); printMsgId("after get MsgId", msg.MessageId); return(msg); }catch (MQException mqe) { throw mqe; }catch (Exception e) { throw new EisException(MQCException.MQ_MSG_RECEIVE_GETMSG_ERROR_EXCEPTION_CODE); } // return null; }
public static Boolean connectMQExce(MQCParameter para, int type) { int interval = getInterval(); int trycount = getCount(); return(connectMQ(para, type, trycount, interval)); }
/** * 连接到MQ队列管理器 * @param para * 调用类的MQ连接配置参数 * @param type * 连接类型,0-接收该Queue队列的消息,1-向该Queue队列发送消息 * @return TRUE:连接成功; * @throws EisException 连接失败则抛出异常 */ public static Boolean connectMQ(MQCParameter para, int type) { try { int interval = getInterval(); int trycount = getCount(); return(connectMQ(para, type, trycount, interval)); } catch (Exception e) { throw new EisException("不能连接到MQ Server :" + para.mqParameter.toString() + "]"); } }
/** * 进行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); }
private void setMQCpara() { cfgSendData = new MQCParameter(getSendQueuePara()); cfgReceiveData = new MQCParameter(getReceiveQueuePara()); }
/** * 连接到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); }