消息隊列(MQ)是一種應用程序對應用程序的通訊方法。應用程序經過寫和檢索出入列隊的針對應用程序的數據(消息)來通訊,而無需專用鏈接來連接它們。java
銀行腳本使用MQ通訊的較多,下面介紹一個MQ的腳本:windows
MQ的腳本分爲SEND和RECIVE兩部分api
send部分:服務器
import com.ibm.mq.*; import lrapi.lr; public class Actions { String PutQueueManagerName = "QMCPG1"; // 發送隊列管理器 String PutQueueName = "HVPSBANK"; // 發送隊列名,至關於前置機的接收隊列 String QueueChannel = "xn_rcv"; // 通道名,要用服務器通道 int PutPort = 1428; // 發送端口號,至關於前置機的接收端口 int CCSID = 1381; // 客戶端Unix用819,windows用1381 int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 鏈接參數 int PutDepth = 0; // 發送隊列深度 String SndTime = ""; // 當前發送時間 MQQueueManager PutQueueManager = null; // 建立發送隊列管理器對象 MQQueue PutQueue = null; // 建立發送隊列對象 MQMessage PutMessage = new MQMessage(); // 建立發送消息對象 MQPutMessageOptions PMO = new MQPutMessageOptions(); // 建立發送消息選項隊列 public int init() throws Throwable { // 發送隊列的參數********************************************** MQEnvironment.hostname = lr.eval_string("<HostIP>"); // 設置環境參數 MQEnvironment.port = PutPort; MQEnvironment.CCSID = CCSID; MQEnvironment.channel = QueueChannel; PutQueueManager = new MQQueueManager(PutQueueManagerName); // 鏈接發送隊列管理器 PutQueue = PutQueueManager.accessQueue(PutQueueName, OpenOptions, null, null, null); // 創建訪問發送隊列 PutMessage.format = MQC.MQFMT_STRING; // 設置消息中應用數據的格式 PutMessage.characterSet = 1381; // 設置字符集 PutMessage.expiry = -1; // 設置消息爲不過時 return 0; }//end of init public int action() throws Throwable { SndTime = String.valueOf(System.currentTimeMillis()); // 獲取當前發送時間,13位 lr.start_transaction("02_CP2I111_大額來賬_發送"); PutQueueMessage("{H:" + "02" + "309391000011 " + "HVPS" + "306581000003 " + "HVPS" + "<Date>" + // 報文發起日期 "<Time>" + // 報文發起時間 "XML" + "hvps.111.001.01 " + SndTime + "<Num>" + // 通訊級標識號,CNAPS2SIMU0000088212,20位,接收方根據OrigSender+OrigSendDate+MesgID惟一肯定一個報文,該三項重複的報文做爲通訊級重複報文; SndTime + "<Num>" + // 通訊級參考號,CNAPS2SIMU0000030415,20位,標識本報文的關聯報文,由OrigSender設置,後續節點應保持該域不變,並在通訊迴應報文中帶回該值,以便OrigSender匹配原報文; "3" + "D" + " " + "}\r\n" + "{S:" + // 數字簽名域起始標識 "2016110700130322|2016-11-07T14:25:01|1|2016110700130322|A100|CNY10.05|NORM|309391000011|309391000011|306581000003|306588000016|321|123|309391000011|306588000016|xingneng|6214620421000208396|02102|" + // 數字簽名內容 "}\r\n" + // 數字簽名域結束標識 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02\">" + "<FIToFICstmrCdtTrf>" + "<GrpHdr>" + "<MsgId>" + "<Date>" + "<Time>" + "<MsgNum>" + "</MsgId>" + // 報文標識號,最大35位,最好是8位日期+8位流水,不然二代後臺會處理異常,2016111015130018 "<CreDtTm>2016-11-07T14:25:01</CreDtTm>" + "<NbOfTxs>1</NbOfTxs>" + "<SttlmInf>" + "<SttlmMtd>CLRG</SttlmMtd>" + "</SttlmInf>" + "</GrpHdr>" + "<CdtTrfTxInf>" + "<PmtId>" + "<EndToEndId>1</EndToEndId>" + "<TxId>" + SndTime + "<Num>" + "</TxId>" + // 交易標識號,最大35位,2016110700130322 "</PmtId>" + "<PmtTpInf>" + "<CtgyPurp>" + "<Prtry>A100</Prtry>" + "</CtgyPurp>" + "</PmtTpInf>" + "<IntrBkSttlmAmt Ccy=\"CNY\">0.02</IntrBkSttlmAmt>" + // 金額 "<SttlmPrty>NORM</SttlmPrty>" + "<ChrgBr>DEBT</ChrgBr>" + "<InstgAgt>" + "<FinInstnId>" + "<ClrSysMmbId>" + "<MmbId>309391000011</MmbId>" + // 付款清算行行號 "</ClrSysMmbId>" + "</FinInstnId>" + "<BrnchId>" + "<Id>309391000011</Id>" + // 收款行行號 "</BrnchId>" + "</InstgAgt>" + "<InstdAgt>" + "<FinInstnId>" + "<ClrSysMmbId>" + "<MmbId>306581000003</MmbId>" + // 收款清算行行號 "</ClrSysMmbId>" + "</FinInstnId>" + "<BrnchId>" + "<Id>306588000016</Id>" + // 收款行行號 "</BrnchId>" + "</InstdAgt>" + "<Dbtr>" + "<Nm>沈監</Nm>" + // 付款人名稱 "</Dbtr>" + "<DbtrAcct>" + "<Id>" + "<Othr>" + "<Id>6225684352000160189</Id>" + // 付款人帳號 "</Othr>" + "</Id>" + "</DbtrAcct>" + "<DbtrAgt>" + "<FinInstnId>" + "<ClrSysMmbId>" + "<MmbId>309391000011</MmbId>" + // 付款人開戶行行號 "</ClrSysMmbId>" + "</FinInstnId>" + "</DbtrAgt>" + "<CdtrAgt>" + "<FinInstnId>" + "<ClrSysMmbId>" + "<MmbId>306588000016</MmbId>" + // 收款人開戶行行號 "</ClrSysMmbId>" + "</FinInstnId>" + "</CdtrAgt>" + "<Cdtr>" + "<Nm>燕淨</Nm>" + // 收款人名稱 "</Cdtr>" + "<CdtrAcct>" + "<Id>" + "<Othr>" + "<Id>6225684341000008415</Id>" + // 收款人帳號 "</Othr>" + "</Id>" + "</CdtrAcct>" + "<Purp>" + "<Prtry>02102</Prtry>" + // 業務種類編碼 "</Purp>" + "<RmtInf>" + "<Ustrd>/C00/2016-11-07</Ustrd>" + // 備註 "</RmtInf>" + "</CdtTrfTxInf>" + "</FIToFICstmrCdtTrf>" + "</Document>"); lr.end_transaction("02_CP2I111_大額來賬_發送",lr.PASS); return 0; }//end of action public int end() throws Throwable { try { PutQueue.close(); PutQueueManager.close(); PutQueueManager.disconnect(); } catch (MQException ex) { lr.error_message("02_CP2I111_大額來賬_發送退出關閉隊列時出現錯誤,完成代碼爲:" + ex.completionCode + ",緣由爲:" + ex.reasonCode); ex.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } return 0; }// end of end // 發送消息++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public void PutQueueMessage(String MyStr) { try { PutMessage = new MQMessage(); PutMessage.write(MyStr.getBytes("UTF-8")); // 呵呵 PutQueue.put(PutMessage, PMO);// 將消息放入隊列 PutQueueManager.commit(); // 提交事務處理 //PutDepth = PutQueue.getCurrentDepth(); // 獲取發送隊列的深度 //System.out.println("++++++發送隊列當前深度爲:"+ PutDepth); //System.out.println("=======發送報文是:" + MyStr); PutMessage.clearMessage(); PutMessage = null; } catch (MQException ex) { lr.end_transaction("02_CP2I111_大額來賬_發送",lr.FAIL); lr.error_message("02_CP2I111_大額來賬_發送_發送消息時出錯,完成代碼爲:" + ex.completionCode + ",緣由爲:" + ex.reasonCode + "流水號爲:" + SndTime); lr.exit(lr.EXIT_ITERATION_AND_CONTINUE, lr.FAIL); //System.out.println("發送消息時出現錯誤,完成代碼爲:" + ex.completionCode + ",緣由爲:" + ex.reasonCode); //ex.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }
RECIVE部分:ui
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import java.util.Date; import com.ibm.mq.*; import lrapi.lr; public class Actions { String GetQueueManagerName = "QMCPG1"; // 接收隊列管理器 String GetQueueName = "306581000003.MBFEA.PBCTOBANK"; // 接收隊列名,至關於前置機的發送隊列 String QueueChannel = "xn_rsp"; // 通道名,要用服務器通道 //String HostName = lr.eval_string("<HostIP>"); // ip 地址 int GetPort = 1428; // 接收端口號,至關於前置機的發送端口 int CCSID = 1381; // 客戶端Unix用819,windows用1381 int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 鏈接參數 int GetDepth = 0; // 接收隊列深度 MQQueueManager GetQueueManager = null; // 建立接收隊列管理器對象 MQQueue GetQueue = null; // 建立接收隊列對象 MQMessage GetMessage = new MQMessage(); // 建立接收消息對象 MQGetMessageOptions GMO = new MQGetMessageOptions(); // 建立接收消息選項對象 public int init() throws Throwable { // 接收隊列的參數*********************************************** MQEnvironment.hostname = lr.eval_string("<HostIP>"); // 設置環境參數 MQEnvironment.port = GetPort; MQEnvironment.CCSID = CCSID; MQEnvironment.channel = QueueChannel; GetQueueManager = new MQQueueManager(GetQueueManagerName); // 鏈接接收隊列管理器 GetQueue = GetQueueManager.accessQueue(GetQueueName, OpenOptions, null , null, null); // 創建訪問接收隊列 GetMessage.format = MQC.MQFMT_STRING; // 設置消息體參數 GetMessage.characterSet = 1381; GetMessage.expiry = -1; GMO.waitInterval = 20000; // 等待時間限制 GMO.options = MQC.MQGMO_NO_WAIT; // 若是隊列沒有消息則當即返回 return 0; }//end of init public int action() throws Throwable { String Msg = null; String NowTime = null; try { GetMessage = new MQMessage(); // 必須新建實例,不然沒法屢次迭代沒法繼續接收消息 GetDepth = GetQueue.getCurrentDepth(); // 獲取接收隊列的深度 if(GetDepth > 0) //接收隊列深度不爲0則打印消息 { GetQueue.get(GetMessage, GMO); GetQueueManager.commit(); //System.out.println("======接收隊列當前深度爲:"+ GetDepth); NowTime = String.valueOf(System.currentTimeMillis()); // 記錄當前時間戳做爲接收時間,13位 Msg = GetMessage.readString(GetMessage.getMessageLength()); CompResult(Msg,NowTime); // 記錄接收時間、流水、交易碼(所接收的交易返回信息不必定爲當前程序發出的交易) //System.out.println("======接收消息的內容爲:\n" + Msg); GetMessage.clearMessage(); GetMessage = null; } else { System.out.println("++++++接收隊列無消息++++++"); } } catch(MQException ex) { if(ex.reasonCode == 2033) { // 2033是沒有消息,不作處理 } else { lr.error_message("02_CP2I111_大額來賬_接收接收消息時出錯,完成代碼爲:" + ex.completionCode + ",緣由爲:" + ex.reasonCode + ",流水號爲:" + NowTime); ex.printStackTrace(); } } catch(Exception e) { e.printStackTrace(); } return 0; }//end of action public int end() throws Throwable { try { GetQueue.close(); // 關閉隊列 GetQueueManager.close(); // 關閉隊列管理器 GetQueueManager.disconnect(); // 斷開鏈接 } catch (MQException ex) { lr.error_message("02_CP2I111_大額來賬_接收關閉隊列時出現錯誤,完成代碼爲:" + ex.completionCode + ",緣由爲:" + ex.reasonCode); ex.printStackTrace(); } return 0; }// end of end // 統計交易結果 public void CompResult(String MSG, String RecTime) { boolean Result = true; // 交易是否成功 String StartTime = null; double DurTime = 0; // 交易處理時間 int FlowIndex = -1; // 位置 String FlowNo = null; int ErrIndex = -1; // 錯誤碼位置 String ErrCode = null; FlowIndex = MSG.indexOf("<MsgId>"); // 流水號的位置 StartTime = MSG.substring(FlowIndex+7, FlowIndex+7+13); // 從流水號獲取開始時間 DurTime = (Double.parseDouble(RecTime) - Double.parseDouble(StartTime))/1000; // 單位秒 //lr.error_message("======處理時間是:" + DurTime + "發送時間是:" + StartTime + "接收時間是:" + RecTime + "返回報文是:" + MSG); ErrIndex = MSG.indexOf("<MsgPrcCd>"); // 錯誤碼的位置 ErrCode = MSG.substring(ErrIndex+10, ErrIndex+10+8); // 錯誤碼 if(!ErrCode.equals("CU0I0000")) // 成功狀態碼是CU0I0000,交易失敗則獲取流水號 { Result = false; FlowNo = MSG.substring(FlowIndex+7, FlowIndex+7+13); // 流水號 lr.error_message("02_CP2I111_大額來賬_接收交易失敗!出錯流水號是:" + FlowNo + ",錯誤碼是:" + ErrCode + ",完整出錯信息是:" + MSG); } // 統計事務響應時間和成功率 if(Result) { lr.set_transaction("02_CP2I111_大額來賬_接收", DurTime, lr.PASS); //lr.error_message("======02_CP2I111_DELZ交易事務處理時間是:" + DurTime); } else { lr.set_transaction("02_CP2I111_大額來賬_接收", DurTime, lr.FAIL); } } } // 正確返回報文: //{H:02306581000003 HVPS309391000011 HVPS20161110164516XMLccms.990.001.02 20161110HVPS00062057012016111016501100013U } //<?xml version="1.0" encoding="UTF-8"?> //<Document xmlns="urn:cnaps:std:ccms:2010:tech:xsd:ccms.990.001.02"><ComConf><ConfInf><OrigSndr>309391000011</OrigSndr><OrigSndDt>20161110</OrigSndDt><MT>hvps.111.001.01</MT><MsgId>01201611101650110001</MsgId><MsgRefId>01201611101650110001</MsgRefId><MsgPrcCd>CU0I0000</MsgPrcCd></ConfInf></ComConf></Document>