[性能测试]:关于MQ协议脚本开发
生活随笔
收集整理的這篇文章主要介紹了
[性能测试]:关于MQ协议脚本开发
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
消息隊列(MQ)是一種應(yīng)用程序?qū)?yīng)用程序的通信方法。應(yīng)用程序通過寫和檢索出入列隊的針對應(yīng)用程序的數(shù)據(jù)(消息)來通信,而無需專用連接來鏈接它們。
銀行腳本使用MQ通信的較多,下面介紹一個MQ的腳本:
MQ的腳本分為SEND和RECIVE兩部分
send部分:
import com.ibm.mq.*; import lrapi.lr;public class Actions {String PutQueueManagerName = "QMCPG1"; // 發(fā)送隊列管理器String PutQueueName = "HVPSBANK"; // 發(fā)送隊列名,相當于前置機的接收隊列String QueueChannel = "xn_rcv"; // 通道名,要用服務(wù)器通道int PutPort = 1428; // 發(fā)送端口號,相當于前置機的接收端口int CCSID = 1381; // 客戶端Unix用819,windows用1381int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 連接參數(shù)int PutDepth = 0; // 發(fā)送隊列深度String SndTime = ""; // 當前發(fā)送時間 MQQueueManager PutQueueManager = null; // 創(chuàng)建發(fā)送隊列管理器對象MQQueue PutQueue = null; // 創(chuàng)建發(fā)送隊列對象MQMessage PutMessage = new MQMessage(); // 創(chuàng)建發(fā)送消息對象MQPutMessageOptions PMO = new MQPutMessageOptions(); // 創(chuàng)建發(fā)送消息選項隊列public int init() throws Throwable{// 發(fā)送隊列的參數(shù)**********************************************MQEnvironment.hostname = lr.eval_string("<HostIP>"); // 設(shè)置環(huán)境參數(shù)MQEnvironment.port = PutPort;MQEnvironment.CCSID = CCSID; MQEnvironment.channel = QueueChannel;PutQueueManager = new MQQueueManager(PutQueueManagerName); // 連接發(fā)送隊列管理器PutQueue = PutQueueManager.accessQueue(PutQueueName, OpenOptions, null, null, null); // 建立訪問發(fā)送隊列PutMessage.format = MQC.MQFMT_STRING; // 設(shè)置消息中應(yīng)用數(shù)據(jù)的格式PutMessage.characterSet = 1381; // 設(shè)置字符集PutMessage.expiry = -1; // 設(shè)置消息為不過期return 0;}//end of initpublic int action() throws Throwable{SndTime = String.valueOf(System.currentTimeMillis()); // 獲取當前發(fā)送時間,13位lr.start_transaction("02_CP2I111_大額來帳_發(fā)送");PutQueueMessage("{H:" +"02" +"309391000011 " +"HVPS" +"306581000003 " +"HVPS" +"<Date>" + // 報文發(fā)起日期"<Time>" + // 報文發(fā)起時間"XML" +"hvps.111.001.01 " +SndTime + "<Num>" + // 通信級標識號,CNAPS2SIMU0000088212,20位,接收方根據(jù)OrigSender+OrigSendDate+MesgID唯一確定一個報文,該三項重復(fù)的報文作為通信級重復(fù)報文;SndTime + "<Num>" + // 通信級參考號,CNAPS2SIMU0000030415,20位,標識本報文的關(guān)聯(lián)報文,由OrigSender設(shè)置,后續(xù)節(jié)點應(yīng)保持該域不變,并在通信回應(yīng)報文中帶回該值,以便OrigSender匹配原報文;"3" +"D" +" " +"}\r\n" +"{S:" + // 數(shù)字簽名域起始標識"2016110700130322|2016-11-07T14:25:01|1|2016110700130322|A100|CNY10.05|NORM|309391000011|309391000011|306581000003|306588000016|321|123|309391000011|306588000016|xingneng|6214620421000208396|02102|" + // 數(shù)字簽名內(nèi)容"}\r\n" + // 數(shù)字簽名域結(jié)束標識"<?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>沈監(jiān)</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>" + // 業(yè)務(wù)種類編碼"</Purp>" +"<RmtInf>" +"<Ustrd>/C00/2016-11-07</Ustrd>" + // 備注"</RmtInf>" +"</CdtTrfTxInf>" +"</FIToFICstmrCdtTrf>" +"</Document>");lr.end_transaction("02_CP2I111_大額來帳_發(fā)送",lr.PASS);return 0;}//end of actionpublic int end() throws Throwable{try{PutQueue.close();PutQueueManager.close();PutQueueManager.disconnect();}catch (MQException ex){lr.error_message("02_CP2I111_大額來帳_發(fā)送退出關(guān)閉隊列時出現(xiàn)錯誤,完成代碼為:" + ex.completionCode + ",原因為:" + ex.reasonCode);ex.printStackTrace();}catch(Exception e){e.printStackTrace();}return 0;}// end of end// 發(fā)送消息++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++public void PutQueueMessage(String MyStr){try{PutMessage = new MQMessage();PutMessage.write(MyStr.getBytes("UTF-8")); // 呵呵PutQueue.put(PutMessage, PMO);// 將消息放入隊列PutQueueManager.commit(); // 提交事務(wù)處理//PutDepth = PutQueue.getCurrentDepth(); // 獲取發(fā)送隊列的深度//System.out.println("++++++發(fā)送隊列當前深度為:"+ PutDepth);//System.out.println("=======發(fā)送報文是:" + MyStr); PutMessage.clearMessage();PutMessage = null;}catch (MQException ex){lr.end_transaction("02_CP2I111_大額來帳_發(fā)送",lr.FAIL);lr.error_message("02_CP2I111_大額來帳_發(fā)送_發(fā)送消息時出錯,完成代碼為:" + ex.completionCode + ",原因為:" + ex.reasonCode + "流水號為:" + SndTime);lr.exit(lr.EXIT_ITERATION_AND_CONTINUE, lr.FAIL);//System.out.println("發(fā)送消息時出現(xiàn)錯誤,完成代碼為:" + ex.completionCode + ",原因為:" + ex.reasonCode);//ex.printStackTrace(); }catch(Exception e){e.printStackTrace();} } }RECIVE部分:
/** 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"; // 接收隊列名,相當于前置機的發(fā)送隊列String QueueChannel = "xn_rsp"; // 通道名,要用服務(wù)器通道//String HostName = lr.eval_string("<HostIP>"); // ip 地址int GetPort = 1428; // 接收端口號,相當于前置機的發(fā)送端口int CCSID = 1381; // 客戶端Unix用819,windows用1381int OpenOptions = MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_OUTPUT|MQC.MQOO_INQUIRE; // 連接參數(shù)int GetDepth = 0; // 接收隊列深度 MQQueueManager GetQueueManager = null; // 創(chuàng)建接收隊列管理器對象MQQueue GetQueue = null; // 創(chuàng)建接收隊列對象MQMessage GetMessage = new MQMessage(); // 創(chuàng)建接收消息對象MQGetMessageOptions GMO = new MQGetMessageOptions(); // 創(chuàng)建接收消息選項對象public int init() throws Throwable{// 接收隊列的參數(shù)***********************************************MQEnvironment.hostname = lr.eval_string("<HostIP>"); // 設(shè)置環(huán)境參數(shù)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; // 設(shè)置消息體參數(shù)GetMessage.characterSet = 1381;GetMessage.expiry = -1;GMO.waitInterval = 20000; // 等待時間限制GMO.options = MQC.MQGMO_NO_WAIT; // 如果隊列沒有消息則立即返回return 0;}//end of initpublic int action() throws Throwable{String Msg = null;String NowTime = null;try{GetMessage = new MQMessage(); // 必須新建實例,否則無法多次迭代無法繼續(xù)接收消息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); // 記錄接收時間、流水、交易碼(所接收的交易返回信息不一定為當前程序發(fā)出的交易)//System.out.println("======接收消息的內(nèi)容為:\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 actionpublic int end() throws Throwable{try{GetQueue.close(); // 關(guān)閉隊列GetQueueManager.close(); // 關(guān)閉隊列管理器GetQueueManager.disconnect(); // 斷開連接 }catch (MQException ex){lr.error_message("02_CP2I111_大額來帳_接收關(guān)閉隊列時出現(xiàn)錯誤,完成代碼為:" + ex.completionCode + ",原因為:" + ex.reasonCode);ex.printStackTrace();}return 0;}// end of end// 統(tǒng)計交易結(jié)果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 + "發(fā)送時間是:" + StartTime + "接收時間是:" + RecTime + "返回報文是:" + MSG);ErrIndex = MSG.indexOf("<MsgPrcCd>"); // 錯誤碼的位置ErrCode = MSG.substring(ErrIndex+10, ErrIndex+10+8); // 錯誤碼if(!ErrCode.equals("CU0I0000")) // 成功狀態(tài)碼是CU0I0000,交易失敗則獲取流水號 {Result = false;FlowNo = MSG.substring(FlowIndex+7, FlowIndex+7+13); // 流水號lr.error_message("02_CP2I111_大額來帳_接收交易失敗!出錯流水號是:" + FlowNo + ",錯誤碼是:" + ErrCode + ",完整出錯信息是:" + MSG);}// 統(tǒng)計事務(wù)響應(yīng)時間和成功率if(Result){lr.set_transaction("02_CP2I111_大額來帳_接收", DurTime, lr.PASS);//lr.error_message("======02_CP2I111_DELZ交易事務(wù)處理時間是:" + 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>?
轉(zhuǎn)載于:https://www.cnblogs.com/fy--/p/8966233.html
總結(jié)
以上是生活随笔為你收集整理的[性能测试]:关于MQ协议脚本开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot-vue项目前台1
- 下一篇: Dungeon Master(三维bfs