java結合WebSphere MQ實現接收隊列檔案功能_java

來源:互聯網
上載者:User

首先我們先來簡單介紹下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.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();    }  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.