Java調用MQ隊列

來源:互聯網
上載者:User

標籤:rup   檔案的   http   name   public   figure   form   說明   reason   

轉載:http://blog.csdn.net/ozwarld/article/details/7735915

IBM MQ 6.0中設定兩個隊列,(遠程隊列、通道之類都不設定)。

隊列管理器是XIR_QM_1502

隊列名稱是ESBREQ

IP地址是10.23.117.134(遠端一台電腦,跟我的電腦不在一個區域網路內)

連接埠1414

CCSID 1208

 

MQ配置可以參考這個,有配圖http://wenku.baidu.com/view/06d108d0360cba1aa811daa3.html

程式如下,發送線程兩個,接收線程一個。接收完畢後就結束。

/*  * 建立日期 2012-7-10  *  * TODO 要更改此產生的檔案的模板,請轉至  * 視窗 - 喜好設定 - Java - 代碼樣式 - 代碼模板  */  package yerasel;    /**  * @author Fenglb E-mail:[email protected]  * @version 建立時間:2009-4-30 下午04:13:38 類說明  */    import java.io.IOException;  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;    interface SomeConstants {      String qManager = "XIR_QM_1502";//"XIR_QM"; //QueueManager name      String qName = "ESBREQ";// Queue Name      String strIP = "10.23.117.134";//"10.24.28.139";//"10.24.28.102";      int iPort = 1502;//1414;      String strChl = "SYSTEM.DEF.SVRCONN";// Server-Connection Channel      int iCCSID = 1208;  }    class Sender implements Runnable, SomeConstants {      public void run() {          sendMessage();      }        public void sendMessage() {            String name = Thread.currentThread().getName();          System.out.println("進入線程" + name);            MQQueueManager qMgr = null;          // configure connection parameters            MQEnvironment.hostname = strIP;          // Server name or IP          MQEnvironment.port = iPort;          MQEnvironment.channel = strChl;          MQEnvironment.CCSID = iCCSID;            // java程式串連mq的方式有兩種,一是客戶機方式,一是綁定方式,          // 預設是客戶機方式,當mq部署在本地的時候,就需要用綁定方式          // 本機IP是10.24.28.139串連10.23.117.134的時候不需要下句          //MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,          //MQC.TRANSPORT_MQSERIES_BINDINGS);            // Create a connection to the QueueManager          System.out.println(name + " Connecting to queue manager: " + qManager);          try {              qMgr = new MQQueueManager(qManager);              // Set up the options on the queue we wish to open              int openOptions = MQC.MQMT_REQUEST | MQC.MQPMO_NEW_MSG_ID                      | MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING                      | MQC.MQOO_INPUT_AS_Q_DEF;              // Now specify the queue that we wish to open and the open options              System.out.println(name + " Accessing queue: " + qName);              MQQueue queue = qMgr.accessQueue(qName, openOptions);              // Define a simple WebSphere MQ Message ...                // Specify the default put message options              MQPutMessageOptions pmo = new MQPutMessageOptions();                // Put the message to the queue              System.out.println(name + " Sending a message...");                MQMessage msg = new MQMessage();              msg.messageId = "MSGID".getBytes();              msg.messageType = MQC.MQMT_REQUEST;              msg.replyToQueueName = "ESBREQ";                // 在此測試一下 mq 的傳輸次列              for (int j = 1; j < 5; j++) {                  msg.messageSequenceNumber = j;                  // write some text in UTF8 format                  try {                      String str = "Salemetsizbe Yerasel";                      str = str + " " + j;                      msg.writeUTF(str);                      queue.put(msg, pmo);                      msg.clearMessage();                      System.out.println(name + " putting the message... " + j);                  } catch (MQException mqe) {                      mqe.printStackTrace();                      break;                  } catch (IOException e1) {                      e1.printStackTrace();                  }              }              qMgr.commit();              System.out.println(name + " Done!");              System.out.println("==========");              System.out.println("");          } catch (MQException e) {              e.printStackTrace();          }      }  }    class Receiver implements Runnable, SomeConstants {        public void run() {          recvMessage();      }        public void recvMessage() {            String name = Thread.currentThread().getName();                    try {              Thread.sleep(1000);              MQQueueManager qMgr = null;                              System.out.println("進入線程" + name);                System.out.println(name + " Connecting to queue manager: "                      + qManager);              qMgr = new MQQueueManager(qManager);              // 設定將要串連的隊列屬性              // Note. The MQC interface defines all the constants used by the              // WebSphere MQ Java programming interface              // (except for completion code constants and error code constants).              // MQOO_INPUT_AS_Q_DEF:Open the queue to get messages using the              // queue-defined default.              // MQOO_OUTPUT:Open the queue to put messages.              int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT                      | MQC.MQOO_INQUIRE;                // Now get the message back again. First define a WebSphere MQ              // message to receive the data              MQMessage rcvMessage = new MQMessage();                // Specify default get message options              MQGetMessageOptions gmo = new MQGetMessageOptions();              gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;// Get messages                                                              // under sync point                                                              // control(在同步點控制下擷取訊息)              gmo.options = gmo.options + MQC.MQGMO_WAIT; // Wait if no messages                                                          // on the                                                          // Queue(如果在隊列上沒有訊息則等待)              gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;// Fail if                                                                      // Qeue                                                                      // Manager                                                                      // Quiescing(如果隊列管理器停頓則失敗)              gmo.waitInterval = 1000; // Sets the time limit for the                                          // wait.(設定等待的毫秒時間限制)                System.out.println(name + " Accessing queue: " + qName);              MQQueue queue = qMgr.accessQueue(qName, openOptions);              int depth = 0;                // Get the message off the queue.              System.out.println("... " + name + " getting the message back again");              for (;;) {                  try {                      queue.get(rcvMessage, gmo);                      System.out.println(" ID: "                              + (new String(rcvMessage.messageId)).trim()                              + " Num: " + rcvMessage.messageSequenceNumber                              + " Type: " + rcvMessage.messageType + " Flag: "                              + rcvMessage.messageFlags);                      // And display the message text...                      String msgText = rcvMessage.readUTF();                      System.out.println("The message is: " + msgText);                      rcvMessage.clearMessage();                        // Break if no MSG left in queue                      depth = queue.getCurrentDepth();                      if (depth == 0)                          break;                    } catch (MQException mqe) {                      mqe.printStackTrace();                      break;                      // null;                  } catch (IOException e) {                      e.printStackTrace();                  }              }              // Close the queue              System.out.println(name + " Closing the queue");              queue.close();              // Disconnect from the QueueManager              System.out.println(name + " Disconnecting from the Queue Manager");              qMgr.disconnect();              System.out.println(name + " Done!");              System.out.println("==========");              System.out.println("");          } catch (MQException ex) {              System.out                      .println("A WebSphere MQ Error occured : Completion Code "                              + ex.completionCode + " Reason Code "                              + ex.reasonCode + ex.getMessage());          } catch (InterruptedException e1) {              e1.printStackTrace();          }      }  }    public class MQTest {        public static void main(String args[]) {            /*          * MQTest first = new MQTest(); first.sendMessage();          * first.recvMessage();          */          Sender sender = new Sender();          Thread senderThread = new Thread(sender);          senderThread.start();          senderThread.setName("Sender");                    Thread senderThread2 = new Thread(sender);          senderThread2.start();          senderThread2.setName("Sender2");                    Receiver recv = new Receiver();          Thread recvThread = new Thread(recv);          recvThread.start();          recvThread.setName("Receiver");            // Receiver recv = new Receiver();          // new Thread(recv).start();        }    }  

  

Java調用MQ隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.