public MQMsgRef receive(int timeout, MQMsgRef mqMsgRef) { if (cfgReceiveData == null) { throw new EisException("Fail to init receive queue."); } MQMessage receiveMsg = getMQMsg(mqMsgRef, timeout); if (receiveMsg == null) { throw new EisException("Fail to get mq msg."); } byte[] response = MQQueueAccesser.getBytesFromMQMessage(receiveMsg); // logUtil.info("replyToQueueManagerName::" + receiveMsg.replyToQueueManagerName); // if (logUtil.isInfoEnabled()) { // StringBuffer sb; // try { // sb = new StringBuffer().append("receive message:\n").append(new String(response, encoding)); // logUtil.info(sb.toString()); // } catch (UnsupportedEncodingException e) { // logUtil.error("Exception:", e); // } // sb = null; // } MQMsgRef mQMsgRef = new MQMsgRef(receiveMsg.MessageId, response); return(mQMsgRef); }
public MQMsgRef send(MQMsgRef msg) { if (cfgSendData == null) { throw new EisException("Fail to init send queue."); } if (msg == null || msg.MQMsgBody == null) { throw new EisException("Error in MQConnection : send message is null."); } // if (logUtil.isInfoEnabled()) { // StringBuffer sb; // try { // sb = new StringBuffer().append("send message:\n").append(new String(msg.MQMsgBody, encoding)); // logUtil.info(sb.toString()); // } catch (UnsupportedEncodingException e) { // logUtil.error("Exception:", e); // } // sb = null; // } MQMessage sendMsg = MQQueueAccesser.getMQMessageFromBytes(msg.MQMsgBody); //设置消息id if (msg != null && msg.MQMsgId != null && msg.MQMsgId.Length > 0) { sendMsg.MessageId = msg.MQMsgId; sendMsg.CorrelationId = msg.MQMsgId; } //设置消息队列管理器 if (msg != null && msg.ReplyToQMgr != null) { sendMsg.ReplyToQueueManagerName = msg.ReplyToQMgr; sendMsg.MessageType = MQC.MQMT_REPLY; } // sendMsg.encoding = cfgSendData.mqParameter.getCcsid(); sendMsg.CharacterSet = cfgSendData.mqParameter.getCcsid(); // sendMsg.format = MQC.MQFMT_STRING; Boolean ret = putMQMsg(sendMsg); if (!ret) { throw new EisException("Fail to put mq msg."); } MQMsgRef mQMsgRef = new MQMsgRef(); mQMsgRef.MQMsgId = sendMsg.MessageId; return(mQMsgRef); }
/** * 进行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 Boolean putMQMsg(MQMessage sendMsg) { try { int i = 0; // while(true){ // try{ // if (cfgSendData.qManager == null || cfgSendData.queue == null) { // initMQConnection(); // } // return MQQueueAccesser.putMsgToQueue(cfgSendData, sendMsg); // }catch(MQException mqe){ // i ++; // int retInt = MQQueueAccesser.handleMQException(mqe); // if (retInt == -1){ // boolean isT = initMQConnection(); // if (!isT) // throw new EisException(MQCException.MQ_CONNECT_ERROR_EXCEPTION_CODE); // // } // // if (i > MQQueueAccesser.getConnPutMsgCount()){ // throw mqe; // } // } // } while (true) { try { if (cfgSendData.qManager == null || cfgSendData.queue == null) { initMQConnection(); } return(MQQueueAccesser.putMsgToQueue(cfgSendData, sendMsg)); } catch (MQException mqe) { LogUtil.Error("putMQMsg error", mqe); int retInt = MQQueueAccesser.handleMQException(mqe); if (retInt == MQQueueAccesser.MQ_CONNECTION_BROKER || retInt == MQQueueAccesser.MQ_UNKNOW_EXCEPTION || retInt == MQQueueAccesser.MQ_CONNECTION_BROKEN_2009) { Boolean isT = initMQConnection(); if (!isT) { throw new EisException(MQCException.MQ_CONNECT_ERROR_EXCEPTION_CODE); } } else { throw mqe; } //对于2009类型的错误因为消息已经发出,所以如果接收到此错误则只是初始化连接,但不重发消息 if (retInt == MQQueueAccesser.MQ_CONNECTION_BROKEN_2009) { throw mqe; } i++; if (i > MQQueueAccesser.getConnPutMsgCount()) { throw mqe; } } } } catch (MQException mqe) { int retInt = MQQueueAccesser.handleMQException(mqe); if (retInt == 1) { throw new EisException(MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_CODE, MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_DESC); } else if (retInt == -1) { throw new EisException(MQCException.MQ_CONNECT_ERROR_EXCEPTION_CODE, MQCException.MQ_CONNECT_ERROR_EXCEPTION_DESC); } else if (retInt == -2) { throw new EisException(MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_CODE, MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_DESC + mqe); } else { throw new EisException(MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_CODE, MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_DESC + mqe); } } catch (Exception e) { throw new EisException(MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_CODE, MQCException.MQ_MSG_SEND_PUTMSG_ERROR_EXCEPTION_DESC); } }
private MQMessage getMQMsg(MQMsgRef mqMsgRef, int timeout) { MQMessage receiveMsg = null; try{ int i = 0; while (true) { try{ if (cfgReceiveData.qManager == null || cfgReceiveData.queue == null) { initMQConnection(); } if (mqMsgRef == null || mqMsgRef.MQMsgId == null || mqMsgRef.MQMsgId.Length <= 0) { receiveMsg = MQQueueAccesser.getMsgFromQueue(null, cfgReceiveData, timeout); } else { receiveMsg = MQQueueAccesser.getMsgFromQueue(mqMsgRef.MQMsgId, cfgReceiveData, timeout); } return(receiveMsg); }catch (MQException mqe) { i++; int ret = MQQueueAccesser.handleMQException(mqe); if (ret == -1) { Boolean isT = initMQConnection(); if (!isT) { throw new EisException(MQCException.MQ_CONNECT_ERROR_EXCEPTION_CODE); } } else if (ret == 1) { throw mqe; } if (i > MQQueueAccesser.getConnGetMsgCount()) { throw mqe; } } } }catch (MQException mqe) { int ret = MQQueueAccesser.handleMQException(mqe); if (ret == 1) { throw new EisException(MQCException.MQ_MSG_RECEIVE_GETMSG_TIMEOUT_EXCEPTION_CODE, MQCException.MQ_MSG_RECEIVE_GETMSG_TIMEOUT_EXCEPTION_DESC); } else if (ret == -1) { throw new EisException(MQCException.MQ_CONNECT_ERROR_EXCEPTION_CODE); } else { throw new EisException(MQCException.MQ_MSG_RECEIVE_GETMSG_ERROR_EXCEPTION_CODE + mqe); } } catch (Exception e) { throw new EisException(MQCException.MQ_MSG_RECEIVE_GETMSG_ERROR_EXCEPTION_CODE, MQCException.MQ_MSG_RECEIVE_GETMSG_ERROR_EXCEPTION_DESC); } }