JMS调用IBM MQ应用--点对点模式

第一篇主要讨论了IBM MQ的安装以及调试样例遇到的几个问题。

这一篇文章主要针对点对点模式来学习一下。学习的样例来源为IBM MQ的sample中的例子。

点对点模式下有一个消息生产者,有消息消费者。一条消息只能消费一次。

JmsProducer 是消息的生产者。

 

Java代码   收藏代码
  1. package test;  
  2.   
  3. // SCCSID "@(#) MQMBID sn=p000-L120604 su=_H-IvIK4nEeGko6IWl3MDhA pn=MQJavaSamples/jms/JmsProducer.java"  
  4. /* 
  5.  *   <copyright  
  6.  *   notice="lm-source-program"  
  7.  *   pids="5724-H72,5655-R36,5655-L82,5724-L26,"  
  8.  *   years="2008,2012"  
  9.  *   crc="279216363" >  
  10.  *  Licensed Materials - Property of IBM   
  11.  *    
  12.  *  5724-H72,5655-R36,5655-L82,5724-L26,  
  13.  *    
  14.  *  (C) Copyright IBM Corp. 2008, 2012 All Rights Reserved.   
  15.  *    
  16.  *  US Government Users Restricted Rights - Use, duplication or   
  17.  *  disclosure restricted by GSA ADP Schedule Contract with   
  18.  *  IBM Corp.   
  19.  *   </copyright>  
  20.  */  
  21.   
  22. import javax.jms.Connection;  
  23. import javax.jms.Destination;  
  24. import javax.jms.JMSException;  
  25. import javax.jms.MessageProducer;  
  26. import javax.jms.Session;  
  27. import javax.jms.TextMessage;  
  28.   
  29. import com.ibm.msg.client.jms.JmsConnectionFactory;  
  30. import com.ibm.msg.client.jms.JmsFactoryFactory;  
  31. import com.ibm.msg.client.wmq.WMQConstants;  
  32.   
  33. /** 
  34.  * A JMS producer (sender or publisher) application that sends a simple message to the named 
  35.  * destination (queue or topic). 
  36.  *  
  37.  * Notes: 
  38.  *  
  39.  * API type: IBM JMS API (v1.1, unified domain) 
  40.  *  
  41.  * Messaging domain: Point-to-point or Publish-Subscribe 
  42.  *  
  43.  * Provider type: WebSphere MQ 
  44.  *  
  45.  * Connection mode: Client connection 
  46.  *  
  47.  * JNDI in use: No 
  48.  *  
  49.  * Usage: 
  50.  *  
  51.  * JmsProducer -m queueManagerName -d destinationName [-h host -p port -l channel] 
  52.  *  
  53.  * for example: 
  54.  *  
  55.  * JmsProducer -m QM1 -d Q1 
  56.  *  
  57.  * JmsProducer -m QM1 -d topic://foo -h localhost -p 1414 
  58.  */  
  59. public class JmsProducer {  
  60.   
  61.   private static String host = "localhost";  
  62.   private static int port = 1414;  
  63.   private static String channel = "SYSTEM.DEF.SVRCONN";  
  64.   private static String queueManagerName = null;  
  65.   private static String destinationName = null;  
  66.   //这里用来判断是不是点对点模式  
  67.   private static boolean isTopic = false;  
  68.   
  69.   // System exit status value (assume unset value to be 1)  
  70.   private static int status = 1;  
  71.   
  72.   /** 
  73.    * Main method 
  74.    *  
  75.    * @param args 
  76.    */  
  77.   public static void main(String[] args) {  
  78.     // Parse the arguments  
  79.     //队列管理器名称如果出现下划线的话会提示( 'MQCC_FAILED' ),原因为 '2058' ( 'MQRC_Q_MGR_NAME_ERROR' )。  
  80.     args = new String[]{"-m","QMTest""-d","testQueue"};  
  81. //  args = new String[]{"-m","aaaa", "-d","topic://zhuti","-h","localhost","-p","14114"};  
  82.     parseArgs(args);  
  83.   
  84.     // Variables  
  85.     Connection connection = null;  
  86.     Session session = null;  
  87.     Destination destination = null;  
  88.     MessageProducer producer = null;  
  89.   
  90.     try {  
  91.       // Create a connection factory  
  92.         //JmsFactoryFactory用来根据指定类型来创建connection factory和destination objects   
  93.       JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);  
  94.       //根据工厂工厂创建连接工厂类的实例  
  95.       JmsConnectionFactory cf = ff.createConnectionFactory();  
  96.   
  97.       // Set the properties  
  98.       //封装连接信息  
  99.       //使用JmsPropertyContext接口中的方法封装信息  
  100.       cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);  
  101.       cf.setIntProperty(WMQConstants.WMQ_PORT, port);  
  102.       //SYSTEM.DEF.SVRCONN是通道的连接类型  
  103.       cf.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);  
  104.       //WMQ_CM_CLIENT的含义,什么时候用,目前还不清楚  
  105.       cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);  
  106.       cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName);  
  107.   
  108.       // Create JMS objects  
  109.       connection = cf.createConnection();  
  110.       session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  111.       if (isTopic) {  
  112.         destination = session.createTopic(destinationName);  
  113.       }  
  114.       else {  
  115.         destination = session.createQueue(destinationName);  
  116.       }  
  117.       producer = session.createProducer(destination);  
  118.   
  119.       long uniqueNumber = System.currentTimeMillis() % 1000;  
  120.       TextMessage message = session.createTextMessage("JmsProducer: Your lucky number today is "  
  121.           + uniqueNumber);  
  122.   
  123.       // Start the connection  
  124.       connection.start();  
  125.   
  126.       // And, send the message  
  127.       producer.send(message);  
  128.       System.out.println("Sent message:\n" + message);  
  129.   
  130.       recordSuccess();  
  131.     }  
  132.     catch (JMSException jmsex) {  
  133.       recordFailure(jmsex);  
  134.     }  
  135.     finally {  
  136.       if (producer != null) {  
  137.         try {  
  138.           producer.close();  
  139.         }  
  140.         catch (JMSException jmsex) {  
  141.           System.out.println("Producer could not be closed.");  
  142.           recordFailure(jmsex);  
  143.         }  
  144.       }  
  145.   
  146.       if (session != null) {  
  147.         try {  
  148.           session.close();  
  149.         }  
  150.         catch (JMSException jmsex) {  
  151.           System.out.println("Session could not be closed.");  
  152.           recordFailure(jmsex);  
  153.         }  
  154.       }  
  155.   
  156.       if (connection != null) {  
  157.         try {  
  158.           connection.close();  
  159.         }  
  160.         catch (JMSException jmsex) {  
  161.           System.out.println("Connection could not be closed.");  
  162.           recordFailure(jmsex);  
  163.         }  
  164.       }  
  165.     }  
  166.     System.exit(status);  
  167.     return;  
  168.   } // end main()  
  169.   
  170.   /** 
  171.    * Process a JMSException and any associated inner exceptions. 
  172.    *  
  173.    * @param jmsex 
  174.    */  
  175.   private static void processJMSException(JMSException jmsex) {  
  176.     System.out.println(jmsex);  
  177.     Throwable innerException = jmsex.getLinkedException();  
  178.     if (innerException != null) {  
  179.       System.out.println("Inner exception(s):");  
  180.     }  
  181.     while (innerException != null) {  
  182.       System.out.println(innerException);  
  183.       innerException = innerException.getCause();  
  184.     }  
  185.     return;  
  186.   }  
  187.   
  188.   /** 
  189.    * Record this run as successful. 
  190.    */  
  191.   private static void recordSuccess() {  
  192.     System.out.println("SUCCESS");  
  193.     status = 0;  
  194.     return;  
  195.   }  
  196.   
  197.   /** 
  198.    * Record this run as failure. 
  199.    *  
  200.    * @param ex 
  201.    */  
  202.   private static void recordFailure(Exception ex) {  
  203.     if (ex != null) {  
  204.       if (ex instanceof JMSException) {  
  205.         processJMSException((JMSException) ex);  
  206.       }  
  207.       else {  
  208.         System.out.println(ex);  
  209.       }  
  210.     }  
  211.     System.out.println("FAILURE");  
  212.     status = -1;  
  213.     return;  
  214.   }  
  215.   
  216.   /** 
  217.    * Parse user supplied arguments. 
  218.    *  
  219.    * @param args 
  220.    */  
  221.   private static void parseArgs(String[] args) {  
  222.     try {  
  223.       int length = args.length;  
  224.       if (length == 0) {  
  225.         throw new IllegalArgumentException("No arguments! Mandatory arguments must be specified.");  
  226.       }  
  227.       if ((length % 2) != 0) {  
  228.         throw new IllegalArgumentException("Incorrect number of arguments!");  
  229.       }  
  230.   
  231.       int i = 0;  
  232.   
  233.       while (i < length) {  
  234.         if ((args[i]).charAt(0) != '-') {  
  235.           throw new IllegalArgumentException("Expected a '-' character next: " + args[i]);  
  236.         }  
  237.   
  238.         char opt = (args[i]).toLowerCase().charAt(1);  
  239.   
  240.         switch (opt) {  
  241.           case 'h' :  
  242.             host = args[++i];  
  243.             break;  
  244.           case 'p' :  
  245.             port = Integer.parseInt(args[++i]);  
  246.             break;  
  247.           case 'l' :  
  248.             channel = args[++i];  
  249.             break;  
  250.           case 'm' :  
  251.             queueManagerName = args[++i];  
  252.             break;  
  253.           case 'd' :  
  254.             destinationName = args[++i];  
  255.             break;  
  256.           default : {  
  257.             throw new IllegalArgumentException("Unknown argument: " + opt);  
  258.           }  
  259.         }  
  260.   
  261.         ++i;  
  262.       }  
  263.   
  264.       if (queueManagerName == null) {  
  265.         throw new IllegalArgumentException("A queueManager name must be specified.");  
  266.       }  
  267.   
  268.       if (destinationName == null) {  
  269.         throw new IllegalArgumentException("A destination name must be specified.");  
  270.       }  
  271.   
  272.       // Whether the destination is a queue or a topic. Apply a simple check.  
  273.       if (destinationName.startsWith("topic://")) {  
  274.         isTopic = true;  
  275.       }  
  276.       else {  
  277.         // Otherwise, let's assume it is a queue.  
  278.         isTopic = false;  
  279.       }  
  280.     }  
  281.     catch (Exception e) {  
  282.       System.out.println(e.getMessage());  
  283.       printUsage();  
  284.       System.exit(-1);  
  285.     }  
  286.     return;  
  287.   }  
  288.   
  289.   /** 
  290.    * Display usage help. 
  291.    */  
  292.   private static void printUsage() {  
  293.     System.out.println("\nUsage:");  
  294.     System.out  
  295.         .println("JmsProducer -m queueManagerName -d destinationName [-h host -p port -l channel]");  
  296.     return;  
  297.   }  
  298.   
  299. // end class  

 

注意:如果还抛( 'MQCC_FAILED' ),原因为 '2035' ( 'MQRC_NOT_AUTHORIZED' )这个异常,请看上一篇文章。

 

下面JmsConsumer。JmsConsumer的连接方式和JmsProducer一样,不再赘述。

JmsProducer和JmsConsumer都需要指出主机名,端口,通道,队列名称和队列。

Java代码   收藏代码
  1. package test;  
  2.   
  3. // SCCSID "@(#) MQMBID sn=p000-L120604 su=_H-IvIK4nEeGko6IWl3MDhA pn=MQJavaSamples/jms/JmsConsumer.java"  
  4. /* 
  5.  *   <copyright  
  6.  *   notice="lm-source-program"  
  7.  *   pids="5724-H72,5655-R36,5655-L82,5724-L26,"  
  8.  *   years="2008,2012"  
  9.  *   crc="39457954" >  
  10.  *  Licensed Materials - Property of IBM   
  11.  *    
  12.  *  5724-H72,5655-R36,5655-L82,5724-L26,  
  13.  *    
  14.  *  (C) Copyright IBM Corp. 2008, 2012 All Rights Reserved.   
  15.  *    
  16.  *  US Government Users Restricted Rights - Use, duplication or   
  17.  *  disclosure restricted by GSA ADP Schedule Contract with   
  18.  *  IBM Corp.   
  19.  *   </copyright>  
  20.  */  
  21.   
  22. import javax.jms.Connection;  
  23. import javax.jms.Destination;  
  24. import javax.jms.JMSException;  
  25. import javax.jms.Message;  
  26. import javax.jms.MessageConsumer;  
  27. import javax.jms.Session;  
  28.   
  29. import com.ibm.msg.client.jms.JmsConnectionFactory;  
  30. import com.ibm.msg.client.jms.JmsFactoryFactory;  
  31. import com.ibm.msg.client.wmq.WMQConstants;  
  32.   
  33. /** 
  34.  * A JMS consumer (receiver or subscriber) application that receives a message from the named 
  35.  * destination (queue or topic). 
  36.  *  
  37.  * Tip: A subscriber application must be started before the publisher application. 
  38.  *  
  39.  * Notes: 
  40.  *  
  41.  * API type: IBM JMS API (v1.1, unified domain) 
  42.  *  
  43.  * Messaging domain: Point-to-point or Publish-Subscribe 
  44.  *  
  45.  * Provider type: WebSphere MQ 
  46.  *  
  47.  * Connection mode: Client connection 
  48.  *  
  49.  * JNDI in use: No 
  50.  *  
  51.  * Usage: 
  52.  *  
  53.  * JmsConsumer -m queueManagerName -d destinationName [-h host -p port -l channel] 
  54.  *  
  55.  * for example: 
  56.  *  
  57.  * JmsConsumer -m QM1 -d Q1 
  58.  *  
  59.  * JmsConsumer -m QM1 -d topic://foo -h localhost -p 1414 
  60.  */  
  61. public class JmsConsumer {  
  62.   
  63.   private static String host = "localhost";  
  64.   private static int port = 1414;  
  65.   private static String channel = "SYSTEM.DEF.SVRCONN";  
  66.   private static String queueManagerName = null;  
  67.   private static String destinationName = null;  
  68.   private static boolean isTopic = false;  
  69.   
  70.   private static int timeout = 15000// in ms or 15 seconds  
  71.   
  72.   // System exit status value (assume unset value to be 1)  
  73.   private static int status = 1;  
  74.   
  75.   /** 
  76.    * Main method 
  77.    *  
  78.    * @param args 
  79.    */  
  80.   public static void main(String[] args) {  
  81. //      args = new String[]{"-m","aaaa", "-d","aa"};  
  82. //      args = new String[]{"-m","aaaa", "-d","topic://zhuti","-h","localhost","-p","1414"};  
  83.     // Parse the arguments  
  84.     args = new String[]{"-m","QMTest""-d","testQueue"};  
  85.     parseArgs(args);  
  86.   
  87.     // Variables  
  88.     Connection connection = null;  
  89.     Session session = null;  
  90.     Destination destination = null;  
  91.     MessageConsumer consumer = null;  
  92.   
  93.     try {  
  94.       // Create a connection factory  
  95.       JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);  
  96.       JmsConnectionFactory cf = ff.createConnectionFactory();  
  97.   
  98.       // Set the properties  
  99.       cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);  
  100.       cf.setIntProperty(WMQConstants.WMQ_PORT, port);  
  101.       cf.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);  
  102.       cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);  
  103.       cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName);  
  104.   
  105.       // Create JMS objects  
  106.       connection = cf.createConnection();  
  107.       session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  108.       if (isTopic) {  
  109.         destination = session.createTopic(destinationName);  
  110.       }  
  111.       else {  
  112.         destination = session.createQueue(destinationName);  
  113.       }  
  114.       consumer = session.createConsumer(destination);  
  115.   
  116.       // Start the connection  
  117.       connection.start();  
  118.   
  119.       // And, receive the message  
  120.       //在指定的超市时间内接收下一条消息  
  121.       Message message = consumer.receive(timeout);  
  122.       if (message != null) {  
  123. //        System.err.println("Received message:\n" + message);  
  124.         System.out.println("Received message:\n" + message);  
  125.       }  
  126.       else {  
  127.         System.out.println("No message received!\n");  
  128.         recordFailure(null);  
  129.       }  
  130.   
  131.       recordSuccess();  
  132.     }  
  133.     catch (JMSException jmsex) {  
  134.       recordFailure(jmsex);  
  135.     }  
  136.     finally {  
  137.       if (consumer != null) {  
  138.         try {  
  139.           consumer.close();  
  140.         }  
  141.         catch (JMSException jmsex) {  
  142.           System.out.println("Consumer could not be closed.");  
  143.           recordFailure(jmsex);  
  144.         }  
  145.       }  
  146.   
  147.       if (session != null) {  
  148.         try {  
  149.           session.close();  
  150.         }  
  151.         catch (JMSException jmsex) {  
  152.           System.out.println("Session could not be closed.");  
  153.           recordFailure(jmsex);  
  154.         }  
  155.       }  
  156.   
  157.       if (connection != null) {  
  158.         try {  
  159.           connection.close();  
  160.         }  
  161.         catch (JMSException jmsex) {  
  162.           System.out.println("Connection could not be closed.");  
  163.           recordFailure(jmsex);  
  164.         }  
  165.       }  
  166.     }  
  167.     System.exit(status);  
  168.     return;  
  169.   } // end main()  
  170.   
  171.   /** 
  172.    * Process a JMSException and any associated inner exceptions. 
  173.    *  
  174.    * @param jmsex 
  175.    */  
  176.   private static void processJMSException(JMSException jmsex) {  
  177.     System.out.println(jmsex);  
  178.     Throwable innerException = jmsex.getLinkedException();  
  179.     if (innerException != null) {  
  180.       System.out.println("Inner exception(s):");  
  181.     }  
  182.     while (innerException != null) {  
  183.       System.out.println(innerException);  
  184.       innerException = innerException.getCause();  
  185.     }  
  186.     return;  
  187.   }  
  188.   
  189.   /** 
  190.    * Record this run as successful. 
  191.    */  
  192.   private static void recordSuccess() {  
  193.     System.out.println("SUCCESS");  
  194.     status = 0;  
  195.     return;  
  196.   }  
  197.   
  198.   /** 
  199.    * Record this run as failure. 
  200.    *  
  201.    * @param ex 
  202.    */  
  203.   private static void recordFailure(Exception ex) {  
  204.     if (ex != null) {  
  205.       if (ex instanceof JMSException) {  
  206.         processJMSException((JMSException) ex);  
  207.       }  
  208.       else {  
  209.         System.out.println(ex);  
  210.       }  
  211.     }  
  212.     System.out.println("FAILURE");  
  213.     status = -1;  
  214.     return;  
  215.   }  
  216.   
  217.   /** 
  218.    * Parse user supplied arguments. 
  219.    *  
  220.    * @param args 
  221.    */  
  222.   private static void parseArgs(String[] args) {  
  223.     try {  
  224.       int length = args.length;  
  225.       if (length == 0) {  
  226.         throw new IllegalArgumentException("No arguments! Mandatory arguments must be specified.");  
  227.       }  
  228.       if ((length % 2) != 0) {  
  229.         throw new IllegalArgumentException("Incorrect number of arguments!");  
  230.       }  
  231.   
  232.       int i = 0;  
  233.   
  234.       while (i < length) {  
  235.         if ((args[i]).charAt(0) != '-') {  
  236.           throw new IllegalArgumentException("Expected a '-' character next: " + args[i]);  
  237.         }  
  238.   
  239.         char opt = (args[i]).toLowerCase().charAt(1);  
  240.   
  241.         switch (opt) {  
  242.           case 'h' :  
  243.             host = args[++i];  
  244.             break;  
  245.           case 'p' :  
  246.             port = Integer.parseInt(args[++i]);  
  247.             break;  
  248.           case 'l' :  
  249.             channel = args[++i];  
  250.             break;  
  251.           case 'm' :  
  252.             queueManagerName = args[++i];  
  253.             break;  
  254.           case 'd' :  
  255.             destinationName = args[++i];  
  256.             break;  
  257.           default : {  
  258.             throw new IllegalArgumentException("Unknown argument: " + opt);  
  259.           }  
  260.         }  
  261.   
  262.         ++i;  
  263.       }  
  264.   
  265.       if (queueManagerName == null) {  
  266.         throw new IllegalArgumentException("A queueManager name must be specified.");  
  267.       }  
  268.   
  269.       if (destinationName == null) {  
  270.         throw new IllegalArgumentException("A destination name must be specified.");  
  271.       }  
  272.   
  273.       // Whether the destination is a queue or a topic. Apply a simple check.  
  274.       if (destinationName.startsWith("topic://")) {  
  275.         isTopic = true;  
  276.       }  
  277.       else {  
  278.         // Otherwise, let's assume it is a queue.  
  279.         isTopic = false;  
  280.       }  
  281.     }  
  282.     catch (Exception e) {  
  283.       System.out.println(e.getMessage());  
  284.       printUsage();  
  285.       System.exit(-1);  
  286.     }  
  287.     return;  
  288.   }  
  289.   
  290.   /** 
  291.    * Display usage help. 
  292.    */  
  293.   private static void printUsage() {  
  294.     System.out.println("\nUsage:");  
  295.     System.out  
  296.         .println("JmsConsumer -m queueManagerName -d destinationName [-h host -p port -l channel]");  
  297.     return;  
  298.   }  
  299.   
  300. // end class  

生产者需要由session创建message producer,消费者需要由session创建message consumer。

 

 

 JMSBrowser和JMSCustomer的区别在于前者只能浏览消息,后者是消费消息。前者能够浏览所有的消息,而后者一次消费一条消息。

 

Java代码   收藏代码
  1. package test;  
  2.   
  3. // SCCSID "@(#) MQMBID sn=p000-L120604 su=_H-IvIK4nEeGko6IWl3MDhA pn=MQJavaSamples/jms/JmsBrowser.java"  
  4. /* 
  5.  *   <copyright  
  6.  *   notice="lm-source-program"  
  7.  *   pids="5724-H72,5655-R36,5655-L82,5724-L26,"  
  8.  *   years="2008,2012"  
  9.  *   crc="3912865343" >  
  10.  *  Licensed Materials - Property of IBM   
  11.  *    
  12.  *  5724-H72,5655-R36,5655-L82,5724-L26,  
  13.  *    
  14.  *  (C) Copyright IBM Corp. 2008, 2012 All Rights Reserved.   
  15.  *    
  16.  *  US Government Users Restricted Rights - Use, duplication or   
  17.  *  disclosure restricted by GSA ADP Schedule Contract with   
  18.  *  IBM Corp.   
  19.  *   </copyright>  
  20.  */  
  21.   
  22. import java.util.Enumeration;  
  23.   
  24. import javax.jms.Connection;  
  25. import javax.jms.JMSException;  
  26. import javax.jms.Message;  
  27. import javax.jms.Queue;  
  28. import javax.jms.QueueBrowser;  
  29. import javax.jms.Session;  
  30.   
  31. import com.ibm.msg.client.jms.JmsConnectionFactory;  
  32. import com.ibm.msg.client.jms.JmsFactoryFactory;  
  33. import com.ibm.msg.client.wmq.WMQConstants;  
  34.   
  35. /** 
  36.  * A JMS queue browser application that looks at all available messages on the named queue, without 
  37.  * removing them, in the order they would be received by a consumer application. 
  38.  *  
  39.  * Tip: A browser is not applicable for topics. 
  40.  *  
  41.  * Notes: 
  42.  *  
  43.  * API type: IBM JMS API (v1.1, unified domain) 
  44.  *  
  45.  * Messaging domain: Point-to-point 
  46.  *  
  47.  * Provider type: WebSphere MQ 
  48.  *  
  49.  * Connection mode: Client connection 
  50.  *  
  51.  * JNDI in use: No 
  52.  *  
  53.  * Usage: 
  54.  *  
  55.  * JmsBrowser -m queueManagerName -d queueName [-h host -p port -l channel] 
  56.  *  
  57.  * for example: 
  58.  *  
  59.  * JmsBrowser -m QM1 -d Q1 
  60.  *  
  61.  * JmsBrowser -m QM1 -d Q1 -h localhost -p 1414 
  62.  */  
  63. public class JmsBrowser {  
  64.   
  65.   private static String host = "localhost";  
  66.   private static int port = 1414;  
  67.   private static String channel = "SYSTEM.DEF.SVRCONN";  
  68.   private static String queueManagerName = null;  
  69.   private static String queueName = null;  
  70.   
  71.   // System exit status value (assume unset value to be 1)  
  72.   private static int status = 1;  
  73.   
  74.   /** 
  75.    * Main method 
  76.    *  
  77.    * @param args 
  78.    */  
  79.   public static void main(String[] args) {  
  80.     // Parse the arguments  
  81.     args = new String[]{"-m","QMTest""-d","testQueue"};  
  82.     parseArgs(args);  
  83.   
  84.     // Variables  
  85.     Connection connection = null;  
  86.     Session session = null;  
  87.     Queue destination = null;  
  88.     QueueBrowser browser = null;  
  89.   
  90.     try {  
  91.       // Create a connection factory  
  92.       JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);  
  93.       JmsConnectionFactory cf = ff.createConnectionFactory();  
  94.   
  95.       // Set the properties  
  96.       cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);  
  97.       cf.setIntProperty(WMQConstants.WMQ_PORT, port);  
  98.       cf.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);  
  99.       cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);  
  100.       cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, queueManagerName);  
  101.   
  102.       // Create JMS objects  
  103.       connection = cf.createConnection();  
  104.       session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  105.       destination = session.createQueue(queueName);  
  106.       browser = session.createBrowser(destination);  
  107.   
  108.       // Start the connection  
  109.       connection.start();  
  110.   
  111.       // And, browse the message  
  112.       //浏览多条消息,并放到枚举类型里  
  113.       Enumeration messages = browser.getEnumeration();  
  114.       int count = 0;  
  115.       Message current;  
  116.       System.out.println("Browse starts");  
  117.       while (messages.hasMoreElements()) {  
  118.         current = (Message) messages.nextElement();  
  119.         System.out.println("\nMessage " + ++count + ":\n");  
  120.         System.out.println(current);  
  121.       }  
  122.       System.out.println("\nNo more messages\n");  
  123.   
  124.       recordSuccess();  
  125.     }  
  126.     catch (JMSException jmsex) {  
  127.       recordFailure(jmsex);  
  128.     }  
  129.     finally {  
  130.       if (browser != null) {  
  131.         try {  
  132.           browser.close();  
  133.         }  
  134.         catch (JMSException jmsex) {  
  135.           System.out.println("Browser could not be closed.");  
  136.           recordFailure(jmsex);  
  137.         }  
  138.       }  
  139.   
  140.       if (session != null) {  
  141.         try {  
  142.           session.close();  
  143.         }  
  144.         catch (JMSException jmsex) {  
  145.           System.out.println("Session could not be closed.");  
  146.           recordFailure(jmsex);  
  147.         }  
  148.       }  
  149.   
  150.       if (connection != null) {  
  151.         try {  
  152.           connection.close();  
  153.         }  
  154.         catch (JMSException jmsex) {  
  155.           System.out.println("Connection could not be closed.");  
  156.           recordFailure(jmsex);  
  157.         }  
  158.       }  
  159.     }  
  160.     System.exit(status);  
  161.     return;  
  162.   } // end main()  
  163.   
  164.   /** 
  165.    * Process a JMSException and any associated inner exceptions. 
  166.    *  
  167.    * @param jmsex 
  168.    */  
  169.   private static void processJMSException(JMSException jmsex) {  
  170.     System.out.println(jmsex);  
  171.     Throwable innerException = jmsex.getLinkedException();  
  172.     if (innerException != null) {  
  173.       System.out.println("Inner exception(s):");  
  174.     }  
  175.     while (innerException != null) {  
  176.       System.out.println(innerException);  
  177.       innerException = innerException.getCause();  
  178.     }  
  179.     return;  
  180.   }  
  181.   
  182.   /** 
  183.    * Record this run as successful. 
  184.    */  
  185.   private static void recordSuccess() {  
  186.     System.out.println("SUCCESS");  
  187.     status = 0;  
  188.     return;  
  189.   }  
  190.   
  191.   /** 
  192.    * Record this run as failure. 
  193.    *  
  194.    * @param ex 
  195.    */  
  196.   private static void recordFailure(Exception ex) {  
  197.     if (ex != null) {  
  198.       if (ex instanceof JMSException) {  
  199.         processJMSException((JMSException) ex);  
  200.       }  
  201.       else {  
  202.         System.out.println(ex);  
  203.       }  
  204.     }  
  205.     System.out.println("FAILURE");  
  206.     status = -1;  
  207.     return;  
  208.   }  
  209.   
  210.   /** 
  211.    * Parse user supplied arguments. 
  212.    *  
  213.    * @param args 
  214.    */  
  215.   private static void parseArgs(String[] args) {  
  216.     try {  
  217.       int length = args.length;  
  218.       if (length == 0) {  
  219.         throw new IllegalArgumentException("No arguments! Mandatory arguments must be specified.");  
  220.       }  
  221.       if ((length % 2) != 0) {  
  222.         throw new IllegalArgumentException("Incorrect number of arguments!");  
  223.       }  
  224.   
  225.       int i = 0;  
  226.   
  227.       while (i < length) {  
  228.         if ((args[i]).charAt(0) != '-') {  
  229.           throw new IllegalArgumentException("Expected a '-' character next: " + args[i]);  
  230.         }  
  231.   
  232.         char opt = (args[i]).toLowerCase().charAt(1);  
  233.   
  234.         switch (opt) {  
  235.           case 'h' :  
  236.             host = args[++i];  
  237.             break;  
  238.           case 'p' :  
  239.             port = Integer.parseInt(args[++i]);  
  240.             break;  
  241.           case 'l' :  
  242.             channel = args[++i];  
  243.             break;  
  244.           case 'm' :  
  245.             queueManagerName = args[++i];  
  246.             break;  
  247.           case 'd' :  
  248.             queueName = args[++i];  
  249.             break;  
  250.           default : {  
  251.             throw new IllegalArgumentException("Unknown argument: " + opt);  
  252.           }  
  253.         }  
  254.   
  255.         ++i;  
  256.       }  
  257.   
  258.       if (queueManagerName == null) {  
  259.         throw new IllegalArgumentException("A queueManager name must be specified.");  
  260.       }  
  261.   
  262.       if (queueName == null) {  
  263.         throw new IllegalArgumentException("A queue name must be specified.");  
  264.       }  
  265.     }  
  266.     catch (Exception e) {  
  267.       System.out.println(e.getMessage());  
  268.       printUsage();  
  269.       System.exit(-1);  
  270.     }  
  271.     return;  
  272.   }  
  273.   
  274.   /** 
  275.    * Display usage help. 
  276.    */  
  277.   private static void printUsage() {  
  278.     System.out.println("\nUsage:");  
  279.     System.out.println("JmsBrowser -m queueManagerName -d queueName [-h host -p port -l channel]");  
  280.     return;  
  281.   }  
  282.   
  283. // end class  

 

 

运行之前当然需要先创建好IBM MQ的服务端,并且创建好队列管理器和队列。

原文地址:http://qiaokeli.iteye.com/blog/1776186

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值