首先我們先來簡單介紹下websphere mq以及安裝使用簡介
websphere mq : 用於傳輸訊息具有跨平台的功能。
1 安裝websphere mq 並啟動
2 websphere mq 建立queue Manager (如:MQSI_SAMPLE_QM)
3 建立queue 類型選擇Local類型的(如lq )
4建立channels 類型選擇Server Connection (如BridgeChannel)
接下來,我們來看實例程式碼:
MQFileReceiver.javapackage com.mq.dpca.file; import java.io.File;import java.io.FileOutputStream; import com.ibm.mq.MQEnvironment;import com.ibm.mq.MQException;import com.ibmqm. MQGetMessageOptions;import com.ibm.mq.MQMessage;import com.ibm.mq.MQQueue;import com.ibm.mq.MQQueueManager;import com.ibm.mq.constants.MQConstants;import com.mq.dpca.msg。 .mq.dpca.util.ReadCmdLine;import com.mq.dpca.util.RenameUtil; /** * * MQ分組接收檔案功能* 主動輪詢*/public class MQFileReceiver { private MQQueueManager qmgr; // 連線到佇列管理器private MQQueue inQueue; // 傳輸佇列佇列佇列String queueName = ""; // 佇列名稱private String host = ""; // private int port = 1414; // 偵聽器的連接埠號碼private String channel = ""; // 頻道名稱private String qmgrName = ""; // 佇列管理器private MQMessage inMsg; // 建立訊息緩衝private MQGetMessageOptions gmo; //設定取得訊息選項private static String fileName = null; // 接收佇列上的訊息並存入檔案private int ccsid = 0; private static String file_dir = null; /** * 程式的入口* * @param args */ public static void main(String args[]) { MQFileReceiver mfs = new MQFileReceiver(String args[]) { MQFileReceiver mfs = new MQFileReceiver(); //初始化連線連接值.initproperty(); //接收檔案mfs.runGoupReceiver(); //取得shell腳本名稱// String shellname = MQConfig.getValueByKey(fileName);// if(shellname!=null&&!"".equals(shellname)){// //呼叫shell// ReadCmdLine.callShell(shellname) ;// }else{// System.out.println("have no shell name,Only receive files.");// } } public void runGoupReceiver() { try { init(); getGroupMessages(); qmgr.commit(); System.out.println("/n Messages successfully Receive "); } catch (MQException mqe) { mqe.printStackTrace(); } catch (MQException mqe) { mqe.printStackTrace(); try { System.out.println("/n Backing out Transaction "); qmgr.backout(); System.exit(2); } catch (Exception e) { e.printStackTrace(); System.exit(2); } } catch (Exception e) { e.printStackTrace(); System.exit (2); } } /** * 初始化伺服器連線資訊* * @throws Exception */ private void init() throws Exception { /*為用戶端連線設定MQEnvironment屬性*/ MQEnvironment.hostname = host; MQEnvironment.channel = channel; MQEnvironment.port = port; /* 連線到佇列管理器*/ qmgr = new MQQueueManager(qmName); 連線到佇列管理器*/ qmgr = new MQQueueManager(qmName);以輸*/ int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING; /* 開啟佇列以輸*/ inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, qmgr.accessQueue(queueName, opnOptn, null, null, nullgr); public void getGroupMessages() { /* 設定取得訊息選項*/ gmo = new MQGetMessageOptions(); gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING; gmo.options = gmo.options + MQConst 等。MINT21; gmo.options = gmo.options + MQConst。 .options + MQConstants.MQGMO_WAIT; /* 設定等待時間限制*/ gmo.waitInterval = 5000; /* 只取得訊息*/ gmo.options = gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE; /* 以輯順序取得訊息* gmo. gmo.options + MQConstants.MQGMO_LOGICAL_ORDER; gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID; /* 建立訊息緩衝*/ inM = new MQMessage(); try { File( (inMsg, gmo); if (fos == null) { try { fileName = inMsg.getStringProperty("fileName"); String fileName_full = null; fileName_full = file_dir + RenameUtil.rename(fileName); fos = new FileFileputStream(new File(fileName_full); = inMsg.getMessageLength(); byte[] buffer = new byte[msgLength]; inMsg.readFully(buffer); fos.write(buffer, 0, msgLength); /* 看是否為最後訊息標識*/ char x = gmo.groupStatus; if (x == MQConstants. MQGS_LAST_MSG_IN_GROUP) { System.out.println("Last Msg in Group"); break; } inMsg.clearMessage(); } catch (Exception e) { System.out .println("Receiver the message without property,do nothing!"); inMsg.clearMessage(); } } else { int msgLength = inMsg.clearMessage(); } } else { int msgLength = inMsg.getMessageLsgth(getMessageLsgth( ); byte[] buffer = new byte[msgLength]; inMsg.readFully(buffer); fos.write(buffer, 0, msgLength); /* 看是否為最後訊息標識*/ char x = gmo.groupStatus; if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.ln ("Last Msg in Group"); break; } inMsg.clearMessage(); } } catch (Exception e) { char x = gmo.groupStatus; if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) { System.out.println("Last Msg in Group"); } break; } } if (fos != null) fos .close(); } catch (Exception e) { System.out.println(e.getMessage()); } } public void initproperty() { MQConfig config = new MQConfig().getInstance(); if (config.getMQ_MANAGER() != null) { qmgrName = config.getMQ_MANAGER(); queueName = config.getMQ_MANAGER(); queueName = config.getM); config.getMQ_CHANNEL(); host = config.getMQ_HOST_NAME(); port = Integer.valueOf(config.getMQ_PROT()); ccsid = Integer.valueOf(config.getMQ_CCSID()); file_dir = config.getFILE_DIR(); } }}