隨筆 - 100  文章 - 50  trackbacks - 0
          <2012年3月>
          26272829123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          這份程序的原始文檔是來自于互聯網,不過不知道作者是誰,在些先謝了,我增加了個人的理解在里面,增加注釋,這樣便于閱讀與理解。

          該程序實現了發送消息與讀取消息的功能,見其中的send***與get***方法。這只適合于測試,因為環境中的程序還需要對此有稍微的更改,在真實的環境中肯定是在while(true){...}的無限循環中去調用其中的get方法,如果有值,那就執行對消息的處理操作,如果沒有值就繼續循環,在get方法中有等待的時間。

          這個程序就其本身來說還是比較理解的:

          1、首先設置一些相關的環境變量

          2、再連接隊列管理器

          3、再次操作隊列管理器中的指定隊列

          4、往指定隊列中發消息或者是從指定對列中取消息

          5、關閉隊列

          如果不知道如何在MQ資源管理器中配置遠程隊列及通過遠程隊列往遠程的MQ發送消息,請參見文章:

          http://blog.csdn.net/fenglibing/archive/2009/05/08/4160639.aspx

          真實環境中的MQ,個人覺得至少都應該有兩個本地隊列加一個遠程隊列,因為消息的交互肯定是相互的,有收消息,肯定也有發消息。一個本地隊列用于接收外部發過來的消息,用法為正常;另一個本地隊例用于傳輸,用于做于遠程隊例的傳輸隊列,將消息發送給遠程主機的本地隊列。要使消息能夠成功的傳送到遠程隊列,還需要配置通道,通常中需要指定遠程通道的IP地址及端口、本地傳輸隊例的名稱、以及本地的通信地址,這樣才能夠往遠程主機發送消息。

          1. /** 
          2.  * @author Fenglb E-mail:56553655@163.com 
          3.  * @version 創建時間:2009-4-30 下午04:13:38 
          4.  * 類說明 
          5.  */  
          6. import java.io.IOException;  
          7. import com.ibm.mq.MQC;  
          8. import com.ibm.mq.MQEnvironment;  
          9. import com.ibm.mq.MQException;  
          10. import com.ibm.mq.MQGetMessageOptions;  
          11. import com.ibm.mq.MQMessage;  
          12. import com.ibm.mq.MQPutMessageOptions;  
          13. import com.ibm.mq.MQQueue;  
          14. import com.ibm.mq.MQQueueManager;  
          15.   
          16. public class MessageByMQ{  
          17.      //定義隊列管理器和隊列的名稱   
          18.      private static String qmName;   
          19.      private static String qName;  
          20.      private static MQQueueManager qMgr;  
          21.      static{  
          22.          //設置環境:   
          23.          //MQEnvironment中包含控制MQQueueManager對象中的環境的構成的靜態變量,MQEnvironment的值的設定會在MQQueueManager的構造函數加載的時候起作用,   
          24.          //因此必須在建立MQQueueManager對象之前設定MQEnvironment中的值.   
          25.          MQEnvironment.hostname="10.24.1.180";          //MQ服務器的IP地址         
          26.          MQEnvironment.channel="S_FENGLB";              //服務器連接的通道   
          27.          MQEnvironment.CCSID=1381;                      //服務器MQ服務使用的編碼1381代表GBK、1208代表UTF(Coded Character Set Identifier:CCSID)   
          28.          MQEnvironment.port=1414;                       //MQ端口   
          29.          qmName = "QM_FENGLB";                          //MQ的隊列管理器名稱   
          30.          qName = "testQ";                               //MQ遠程隊列的名稱   
          31.          try {  
          32.              //定義并初始化隊列管理器對象并連接    
          33.              //MQQueueManager可以被多線程共享,但是從MQ獲取信息的時候是同步的,任何時候只有一個線程可以和MQ通信。   
          34.             qMgr = new MQQueueManager(qmName);  
          35.         } catch (MQException e) {  
          36.             // TODO Auto-generated catch block   
          37.             System.out.println("初使化MQ出錯");  
          38.             e.printStackTrace();  
          39.         }   
          40.      }  
          41.      /** 
          42.       * 往MQ發送消息 
          43.       * @param message 
          44.       * @return 
          45.       */  
          46.      public static int sendMessage(String message){  
          47.          int result=0;  
          48.          try{     
          49.              //設置將要連接的隊列屬性   
          50.              // Note. The MQC interface defines all the constants used by the WebSphere MQ Java programming interface    
          51.              //(except for completion code constants and error code constants).   
          52.              //MQOO_INPUT_AS_Q_DEF:Open the queue to get messages using the queue-defined default.   
          53.              //MQOO_OUTPUT:Open the queue to put messages.   
          54.              /*目標為遠程隊列,所有這里不可以用MQOO_INPUT_AS_Q_DEF屬性*/  
          55.              //int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;   
          56.              /*以下選項可適合遠程隊列與本地隊列*/  
          57.              int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;  
          58.              //連接隊列    
          59.              //MQQueue provides inquire, set, put and get operations for WebSphere MQ queues.    
          60.              //The inquire and set capabilities are inherited from MQManagedObject.    
          61.              /*關閉了就重新打開*/  
          62.             if(qMgr==null || !qMgr.isConnected()){  
          63.                 qMgr = new MQQueueManager(qmName);  
          64.             }  
          65.              MQQueue queue = qMgr.accessQueue(qName, openOptions);            
          66.              //定義一個簡單的消息   
          67.              MQMessage putMessage = new MQMessage();   
          68.              //將數據放入消息緩沖區   
          69.              putMessage.writeUTF(message);    
          70.              //設置寫入消息的屬性(默認屬性)   
          71.              MQPutMessageOptions pmo = new MQPutMessageOptions();             
          72.              //將消息寫入隊列    
          73.              queue.put(putMessage,pmo);   
          74.              queue.close();  
          75.          }catch (MQException ex) {   
          76.              System.out.println("A WebSphere MQ error occurred : Completion code "   
          77.              + ex.completionCode + " Reason code " + ex.reasonCode);   
          78.              ex.printStackTrace();  
          79.          }catch (IOException ex) {   
          80.              System.out.println("An error occurred whilst writing to the message buffer: " + ex);   
          81.          }catch(Exception ex){  
          82.              ex.printStackTrace();  
          83.          }finally{  
          84.              try {  
          85.                 qMgr.disconnect();  
          86.             } catch (MQException e) {  
          87.                 e.printStackTrace();  
          88.             }  
          89.           }  
          90.          return result;  
          91.      }  
          92.      /** 
          93.       * 從隊列中去獲取消息,如果隊列中沒有消息,就會發生異常,不過沒有關系,有TRY...CATCH,如果是第三方程序調用方法,如果無返回則說明無消息 
          94.       * 第三方可以將該方法放于一個無限循環的while(true){...}之中,不需要設置等待,因為在該方法內部在沒有消息的時候會自動等待。 
          95.       * @return 
          96.       */  
          97.      public static String getMessage(){  
          98.          String message=null;  
          99.          try{              
          100.              //設置將要連接的隊列屬性   
          101.              // Note. The MQC interface defines all the constants used by the WebSphere MQ Java programming interface    
          102.              //(except for completion code constants and error code constants).   
          103.              //MQOO_INPUT_AS_Q_DEF:Open the queue to get messages using the queue-defined default.   
          104.              //MQOO_OUTPUT:Open the queue to put messages.   
          105.              int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;  
          106.              MQMessage retrieve = new MQMessage();  
          107.              //設置取出消息的屬性(默認屬性)   
          108.              //Set the put message options.(設置放置消息選項)    
          109.              MQGetMessageOptions gmo = new MQGetMessageOptions();   
          110.              gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;//Get messages under sync point control(在同步點控制下獲取消息)    
          111.              gmo.options = gmo.options + MQC.MQGMO_WAIT;  // Wait if no messages on the Queue(如果在隊列上沒有消息則等待)    
          112.              gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;// Fail if Qeue Manager Quiescing(如果隊列管理器停頓則失敗)    
          113.              gmo.waitInterval = 1000 ;  // Sets the time limit for the wait.(設置等待的毫秒時間限制)    
          114.              /*關閉了就重新打開*/  
          115.             if(qMgr==null || !qMgr.isConnected()){  
          116.                 qMgr = new MQQueueManager(qmName);  
          117.             }  
          118.              MQQueue queue = qMgr.accessQueue(qName, openOptions);   
          119.              // 從隊列中取出消息   
          120.              queue.get(retrieve, gmo);  
          121.              message = retrieve.readUTF();    
          122.              System.out.println("The message is: " + message);   
          123.              queue.close();  
          124.          }catch (MQException ex) {   
          125.              System.out.println("A WebSphere MQ error occurred : Completion code "   
          126.              + ex.completionCode + " Reason code " + ex.reasonCode);   
          127.          }catch (IOException ex) {   
          128.              System.out.println("An error occurred whilst writing to the message buffer: " + ex);   
          129.          }catch(Exception ex){  
          130.              ex.printStackTrace();  
          131.          }finally{  
          132.              try {  
          133.                 qMgr.disconnect();  
          134.             } catch (MQException e) {  
          135.                 e.printStackTrace();  
          136.             }  
          137.          }  
          138.          return message;  
          139.      }  
          140.      public static void main(String args[]) {  
          141.          /*下面兩個方法可同時使用,也可以單獨使用*/  
          142.          sendMessage("this is a test");  
          143.          //getMessage();   
          144.      }  
          145. }  
          posted on 2012-03-04 23:15 fly 閱讀(3998) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 潢川县| 通许县| 喀喇| 工布江达县| 德格县| 竹北市| 教育| 阿尔山市| 苏尼特左旗| 定边县| 阳山县| 山东省| 金门县| 洮南市| 黎平县| 沈阳市| 昌宁县| 大洼县| 叙永县| 承德县| 湖州市| 嵩明县| 江城| 牡丹江市| 平武县| 绥江县| 台中县| 石台县| 仙游县| 汉寿县| 武汉市| 饶平县| 商丘市| 长沙市| 灵宝市| 江西省| 和顺县| 焉耆| 纳雍县| 长沙市| 天气|