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

分手后的思念是犯贱 2022-05-30 09:30 308阅读 0赞

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

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

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

JmsProducer 是消息的生产者。

Java代码 收藏代码

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

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

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

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

Java代码 收藏代码

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

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

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

Java代码 收藏代码

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

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

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

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JMSMQ

    1 JMS与MQ  1.1 JMS JMS(Java Messaging Service)是Java平台上有关面向消息中间件(MOM)的技术规范,它便于消息系统中的

    相关 IBM MQ简单开发和应用

    IBM MQ经常被一些政府公共部门,银行等企业用来做数据传输和报文收发,在互联网应用的开发中较少见到,属于一种比较老旧的应用。这里以IBM Websphere MQ 7.5版本

    相关 JMS--、发布订阅

    点对点 点对点模型是基于队列的,生产者发消息到队列,消费者从队列接收消息,队列的存在使得消息的异步传输成为可能。和我们平时给朋友发送短信类似。 1. 如果在Sessi