⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mq.java

📁 一个简单的IBM MQ操作例子
💻 JAVA
字号:
package demo;

import com.ibm.mq.*;
//please include mq jars first

public class FirstMqTest
{
public static void main(String args[]){
  FirstMqTest first = new FirstMqTest();
  first.test();
  
}
public void test(){
  String qManager = "MYQUEUE"; //QueueManager name
    String qName = "PUB_SENDQ";//Queue Name
    try {
              //configure connection parameters
              MQEnvironment.hostname="127.0.0.1";//MQ Server name or IP
              MQEnvironment.port=1414;
              MQEnvironment.channel="CHN_SVRCONN";//Server-Connection Channel
              MQEnvironment.CCSID =1381;
              // Create a connection to the QueueManager
              System.out.println("Connecting to queue manager: "+qManager);
              MQQueueManager 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("Accessing queue: "+qName);
              MQQueue queue = qMgr.accessQueue(qName, openOptions);
              // Define a simple WebSphere MQ Message ... 

              // ... and write some text in UTF8 format
              //msg.writeUTF("Hello");
              // Specify the default put message options
              MQPutMessageOptions pmo = new MQPutMessageOptions();
              // Put the message to the queue
              System.out.println("Sending a message...");
              MQMessage msg = new MQMessage();
              
              msg.messageId="0001".getBytes();   
              msg.messageType=MQC.MQMT_REQUEST;
              msg.replyToQueueName="PUB_RECEVIEQ";
              
               /* 在此测试一下 mq 的传输次列
               * 
               */
               for(int j=1;j<5000;j++){
            	   //msg.messageSequenceNumber=j;
            	   
            	   //MQMessage msg = new MQMessage();
            	   try
            	   {
                   String str ="test 你好 hsssssssssssssssssssssssssssssssssssssssssssshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhggggggggggggggggggggggggggggggggggggggggggggggggggggggggg";
                   str = str+j;
                   msg.writeUTF(str);
                   queue.put(msg, pmo);
                   msg.clearMessage();
                   System.out.println("putting the message...");
            	   }catch(MQException mqe)
                   {
                 	  mqe.printStackTrace() ;
                 	  break;
                 	  //null;
                   }
               }
               qMgr.commit();

              //queue.put(msg, pmo);
              // 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();
              // Get the message off the queue.
              System.out.println("...and getting the message back again");
              for(;;)
              {
            	  
              try
              {
            	  
              MQMessage rcvMessage = new MQMessage();
              
              queue.get(rcvMessage, gmo);
              System.out.println(" ID:"+new String(rcvMessage.messageId)+" 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();
              char x = gmo.groupStatus ;
              if(x==MQC.MQGS_LAST_MSG_IN_GROUP)
            	  break;

              }catch(MQException mqe)
              {
            	  //mqe.printStackTrace() ;
            	  break;
            	  //null;
              }
              }
              // Close the queue
              System.out.println("Closing the queue");
              queue.close();
              // Disconnect from the QueueManager
              System.out.println("Disconnecting from the Queue Manager");
              qMgr.disconnect();
              System.out.println("Done!");
          }
          catch (MQException ex) {
              System.out.println("A WebSphere MQ Error occured : Completion Code "
                      + ex.completionCode + " Reason Code " + ex.reasonCode +ex.getMessage());
          }
          catch (java.io.IOException ex) {
              System.out.println("An IOException occured whilst writing to the message buffer: "
                      + ex);
          }catch(Exception ex)
          {
              System.out.println(ex.getMessage());        	  
          }

}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -