java聯合WebSphere MQ完成吸收隊列文件功效。本站提示廣大學習愛好者:(java聯合WebSphere MQ完成吸收隊列文件功效)文章只能為提供參考,不一定能成為您想要的結果。以下是java聯合WebSphere MQ完成吸收隊列文件功效正文
起首我們先來簡略引見下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.java package com.mq.dpca.file; import java.io.File; import java.io.FileOutputStream; import com.ibm.mq.MQEnvironment; import com.ibm.mq.MQException; import com.ibm.mq.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.MQConfig; import com.mq.dpca.util.ReadCmdLine; import com.mq.dpca.util.RenameUtil; /** * * MQ分組吸收文件功效 * 自動輪詢 */ public class MQFileReceiver { private MQQueueManager qmgr; // 銜接到隊列治理器 private MQQueue inQueue; // 傳輸隊列 private 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(); //初始化銜接 mfs.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(); 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(qmgrName); /* 設置隊列翻開選項以輸 */ int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_FAIL_IF_QUIESCING; /* 翻開隊列以輸 */ inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null); } /** * 接收文件的主函數 * * @throws Exception */ public void getGroupMessages() { /* 設置獲得新聞選項 */ gmo = new MQGetMessageOptions(); gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING; gmo.options = gmo.options + MQConstants.MQGMO_SYNCPOINT; /* 期待新聞 */ gmo.options = gmo.options + MQConstants.MQGMO_WAIT; /* 設置期待時光限制 */ gmo.waitInterval = 5000; /* 只獲得新聞 */ gmo.options = gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE; /* 以輯次序獲得新聞 */ gmo.options = gmo.options + MQConstants.MQGMO_LOGICAL_ORDER; gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID; /* 創立新聞緩沖 */ inMsg = new MQMessage(); try { FileOutputStream fos = null; /* 處置組新聞 */ while (true) { try { inQueue.get(inMsg, gmo); if (fos == null) { try { fileName = inMsg.getStringProperty("fileName"); String fileName_full = null; fileName_full = file_dir + RenameUtil.rename(fileName); fos = new FileOutputStream(new File(fileName_full)); int msgLength = 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.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) { 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_QUEUE_NAME(); channel = 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(); } } }