由于最近開發(fā)一個MQ接口項目,故用到了IBM的Websphere MQ中間件,關于MQ的一些常規(guī)性知識我這里不作敘述,這些也不屬本站經驗篇范疇之內,MQ支持眾多語言,本文是JAVA的API?,F(xiàn)以列表體現(xiàn):
1、給消息定義標識(ID)的方法:
?? //使發(fā)送的消息標識為 200601??
??? String id="200601";??
??? MQMessage inMsg = new MQMessage(); //創(chuàng)建消息緩沖區(qū)
??? inMsg.messageId=id.getBytes();
??? String msgString=null;????
??? queue.set(inMsg,gmo);
??? //接受標識為 200601的消息,其它的將不接受。
?? ?MQMessage inMsg = new MQMessage(); //創(chuàng)建消息緩沖區(qū)
??? inMsg.messageId=id.getBytes();
??? String msgString=null;????
??? queue.get(inMsg,gmo);
? 以messageId來給消息一個標識,這個標識是字符型的,即String類型的。
2、正確讀取整條消息內容
?? String msgString;
?? msgString=inMsg.readStringOfByteLength(inMsg.getDataLength());
??? inMsg.messageId=id.getBytes();
??? String msgString=null;
????queue.get(inMsg,gmo);
? ? //msgString=inMsg.readUTF();
????String msgString;
??? msgString=inMsg.readStringOfByteLength(inMsg.getDataLength());
?? 讀取消息常用的方法有:
??? readString(長度)
??? readStringOfByteLength(長度)
??? //以上兩個方法為適用于讀出整個消息
??? readLine()
??? //讀取一行
? 大多數(shù)教程中,均使用readString方法,但這個方法有些問題,現(xiàn)在不推薦使用,有些文件可以讀,有些文件由于其長度getDataLength獲得不對,所以導致出錯。所以,我們用readStringOfByteLength來獲得整個消息。其參數(shù)可以為消息的長度。
3、獲得消息內容的長度:
? getDataLength
? getMessageLength
? 這兩個方法都是獲得消息長度,getDataLength是未讀出的消息長度,比較,你已經用read方法讀取了2000長度的消息,那么getDataLength就是消息總找度減去已讀的這部分。而getMessageLength是整個消息的長度。無論是否read它的值是不變的。
inMsg.messageId=id.getBytes();
??? String msgString=null;
???
???
? queue.get(inMsg,gmo);
?
?//String k=new String(inMsg.messageId,"gb2312");
?//System.out.println(k.trim());
?
? //msgString=inMsg.readUTF();
? String msg;
? msgString=inMsg.readStringOfByteLength(inMsg.getDataLength());
?
? System.out.println("消息:\n" + msgString);
? System.out.println("\n消息長度:\n" + inMsg.getMessageLength());
? System.out.println("\n消息長度getDataLength:\n" + inMsg.getDataLength());
? qMgr.commit();//提交事務處理