The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          import com.ibm.mq.MQC;
          import com.ibm.mq.MQEnvironment;
          import com.ibm.mq.MQException;
          import com.ibm.mq.MQGetMessageOptions;
          import com.ibm.mq.MQMessage;
          import com.ibm.mq.MQPutMessageOptions;
          import com.ibm.mq.MQQueue;
          import com.ibm.mq.MQQueueManager;

          /**//*
           * 可以在MQ的資源管理器的某一個隊列上放入測試消息、瀏覽消息等
           * 可以放入多條消息,按先進先出的方式取得
           
          */
          public class MQTest {

              
          private String qManager;// QueueManager名

              
          private MQQueueManager qMgr;

              
          private MQQueue qQueue;

              String HOST_NAME;

              
          int PORT = 0;

              String Q_NAME;

              String CHANNEL;

              
          int CCSID;

              String Msg;

              
          public void init() {

                  
          try {
                      HOST_NAME 
          = "192.168.88.169";
                      PORT 
          = 1414;
                      qManager 
          = "QM_yfgaochun";
                      Q_NAME 
          = "QM_NAME1";
                      CHANNEL 
          = "S_yfgaochun";
                      CCSID 
          = 1381
                      
          // 表示是簡體中文,
                      
          // CCSID的值在AIX上一般設為1383,如果要支持GBK則設為1386,在WIN上設為1381。

                      Msg 
          = "MQ測試發(fā)送Gao";
                      
          // System.out.println(Msg);

                      MQEnvironment.hostname 
          = HOST_NAME; // 安裝MQ所在的ip address
                      MQEnvironment.port = PORT; // TCP/IP port

                      MQEnvironment.channel 
          = CHANNEL;
                      MQEnvironment.CCSID 
          = CCSID;

                      qMgr 
          = new MQQueueManager(qManager);

                   
                      
          int qOptioin = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE
                              
          | MQC.MQOO_OUTPUT;

                      qQueue 
          = qMgr.accessQueue(Q_NAME, qOptioin);

                  } 
          catch (MQException e) {
                      System.out
                              .println(
          "A WebSphere MQ error occurred : Completion code "
                                      
          + e.completionCode + " Reason Code is "
                                      
          + e.reasonCode);
                  }
              }

              
          void finalizer() {
                  
          try {
                      qQueue.close();
                      qMgr.disconnect();
                  } 
          catch (MQException e) {
                      System.out
                              .println(
          "A WebSphere MQ error occurred : Completion code "
                                      
          + e.completionCode + " Reason Code is "
                                      
          + e.reasonCode);
                  }
              }

              
          /**//*
                   * 取過一次,下次就沒有了
                   
          */
              
          public void GetMsg() {
                  
          try {
                      MQMessage retrievedMessage 
          = new MQMessage();

                      MQGetMessageOptions gmo 
          = new MQGetMessageOptions();
                      gmo.options 
          += MQC.MQPMO_SYNCPOINT;

                      qQueue.get(retrievedMessage, gmo);

                      
          int length = retrievedMessage.getDataLength();

                      
          byte[] msg = new byte[length];

                      retrievedMessage.readFully(msg);

                      String sMsg 
          = new String(msg);
                      System.out.println(sMsg);

                  } 
          catch (RuntimeException e) {
                      e.printStackTrace();
                  } 
          catch (MQException e) {
                      
          if (e.reasonCode != 2033// 沒有消息
                      {
                          e.printStackTrace();
                          System.out
                                  .println(
          "A WebSphere MQ error occurred : Completion code "
                                          
          + e.completionCode
                                          
          + " Reason Code is "
                                          
          + e.reasonCode);
                      }
                  } 
          catch (java.io.IOException e) {
                      System.out
                              .println(
          "An error occurred whilst to the message buffer "
                                      
          + e);
                  }
              }

              
          public void SendMsg(byte[] qByte) {
                  
          try {
                      MQMessage qMsg 
          = new MQMessage();
                      qMsg.write(qByte);
                      MQPutMessageOptions pmo 
          = new MQPutMessageOptions();

                      qQueue.put(qMsg, pmo);

                      System.out.println(
          "The message is sent!");
                      System.out.println(
          "\tThe message is " + new String(qByte, "GBK"));
                  } 
          catch (MQException e) {
                      System.out
                              .println(
          "A WebSphere MQ error occurred : Completion code "
                                      
          + e.completionCode + " Reason Code is "
                                      
          + e.reasonCode);
                  } 
          catch (java.io.IOException e) {
                      System.out
                              .println(
          "An error occurred whilst to the message buffer "
                                      
          + e);
                  }

              }

              
          /** */
              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          // TODO Auto-generated method stub
                  MQTest mqst = new MQTest();
                  mqst.init();
                  
          try {
                      mqst.SendMsg(mqst.Msg.getBytes(
          "GBK"));
                      
          // mqst.GetMsg();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  mqst.finalizer();
              }

          }
          posted on 2013-10-24 17:08 Eric_jiang 閱讀(574) 評論(1)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 遂川县| 巴楚县| 合川市| 江门市| 宜黄县| 连平县| 东辽县| 财经| 苏州市| 潮安县| 九龙城区| 平罗县| 江源县| 武宣县| 博白县| 龙岩市| 余干县| 潜山县| 奉贤区| 句容市| 辰溪县| 安庆市| 遂溪县| 竹山县| 怀仁县| 通辽市| 贡觉县| 宜良县| 闽侯县| 永济市| 高要市| 辽源市| 上思县| 客服| 长岛县| 比如县| 镇宁| 绥棱县| 武隆县| 吉林省| 衡阳市|