{"id":5463,"date":"2019-05-23T17:01:35","date_gmt":"2019-05-23T21:01:35","guid":{"rendered":"https:\/\/www.capitalware.com\/rl_blog\/?p=5463"},"modified":"2019-05-30T13:33:07","modified_gmt":"2019-05-30T17:33:07","slug":"java-mq-code-to-list-all-local-queues-and-their-attributes","status":"publish","type":"post","link":"https:\/\/www.capitalware.com\/rl_blog\/?p=5463","title":{"rendered":"Java MQ Code to List All Local Queues and their Attributes"},"content":{"rendered":"<p>If you have done the following runmqsc command to display all local queues with attributes of a queue manager:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">DIS QL(*) ALL<\/pre>\n<p>And you wanted to do the same thing via a program, here is a fully functioning Java MQ example that will connect to a remote queue manager, issue a PCF &#8220;Inquire Queue&#8221; command, get the PCF response messages, loop through the PCF responses and output the information.  You can download the source code from <strong><a href=\"https:\/\/www.capitalware.com\/dl\/code\/java\/mqlistqueueattributes01.zip\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a><\/strong>.<\/p>\n<p>For more information about the PCF &#8220;Inquire Queue&#8221; command, go to MQ KnowLedge Center <a href=\"https:\/\/www.ibm.com\/support\/knowledgecenter\/en\/SSFKSJ_9.1.0\/com.ibm.mq.ref.adm.doc\/q087800_.htm\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import java.io.IOException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\nimport java.util.Hashtable;\r\n\r\nimport com.ibm.mq.MQException;\r\nimport com.ibm.mq.MQQueueManager;\r\nimport com.ibm.mq.constants.CMQC;\r\nimport com.ibm.mq.constants.CMQCFC;\r\nimport com.ibm.mq.headers.MQDataException;\r\nimport com.ibm.mq.headers.pcf.PCFMessage;\r\nimport com.ibm.mq.headers.pcf.PCFMessageAgent;\r\n\r\n\/**\r\n * Program Name\r\n *  MQListQueueAttributes01\r\n *\r\n * Description\r\n *  This java class issues a PCF &quot;inquire queue&quot; request message for all (&quot;*&quot;) local queues \r\n *  of a remote queue manager. \r\n *  \r\n *  This PCF code is equivalent to issuing the runmqsc command of:\r\n *     DIS QL(*) ALL \r\n *\r\n * Sample Command Line Parameters\r\n *  -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -u UserID -x Password\r\n *\r\n * @author Roger Lacroix\r\n *\/\r\npublic class MQListQueueAttributes01\r\n{\r\n   private static final SimpleDateFormat  LOGGER_TIMESTAMP = new SimpleDateFormat(&quot;yyyy\/MM\/dd HH:mm:ss.SSS&quot;);\r\n\r\n   private Hashtable&lt;String,String&gt; params;\r\n   private Hashtable&lt;String,Object&gt; mqht;\r\n   private String qMgrName;\r\n\r\n   public MQListQueueAttributes01()\r\n   {\r\n      super();\r\n      params = new Hashtable&lt;String,String&gt;();\r\n      mqht = new Hashtable&lt;String,Object&gt;();\r\n   }\r\n\r\n   \/**\r\n    * Make sure the required parameters are present.\r\n    * @return true\/false\r\n    *\/\r\n   private boolean allParamsPresent()\r\n   {\r\n      boolean b = params.containsKey(&quot;-h&quot;) &amp;&amp; params.containsKey(&quot;-p&quot;) &amp;&amp;\r\n                  params.containsKey(&quot;-c&quot;) &amp;&amp; params.containsKey(&quot;-m&quot;) &amp;&amp;\r\n                  params.containsKey(&quot;-u&quot;) &amp;&amp; params.containsKey(&quot;-x&quot;);\r\n      if (b)\r\n      {\r\n         try\r\n         {\r\n            Integer.parseInt((String) params.get(&quot;-p&quot;));\r\n         }\r\n         catch (NumberFormatException e)\r\n         {\r\n            b = false;\r\n         }\r\n      }\r\n\r\n      return b;\r\n   }\r\n\r\n   \/**\r\n    * Extract the command-line parameters and initialize the MQ HashTable.\r\n    * @param args\r\n    * @throws IllegalArgumentException\r\n    *\/\r\n   private void init(String&#x5B;] args) throws IllegalArgumentException\r\n   {\r\n      int port = 1414;\r\n      if (args.length &gt; 0 &amp;&amp; (args.length % 2) == 0)\r\n      {\r\n         for (int i = 0; i &lt; args.length; i += 2)\r\n         {\r\n            params.put(args&#x5B;i], args&#x5B;i + 1]);\r\n         }\r\n      }\r\n      else\r\n      {\r\n         throw new IllegalArgumentException();\r\n      }\r\n\r\n      if (allParamsPresent())\r\n      {\r\n         qMgrName = (String) params.get(&quot;-m&quot;);\r\n         \r\n         try\r\n         {\r\n            port = Integer.parseInt((String) params.get(&quot;-p&quot;));\r\n         }\r\n         catch (NumberFormatException e)\r\n         {\r\n            port = 1414;\r\n         }\r\n         \r\n         mqht.put(CMQC.CHANNEL_PROPERTY, params.get(&quot;-c&quot;));\r\n         mqht.put(CMQC.HOST_NAME_PROPERTY, params.get(&quot;-h&quot;));\r\n         mqht.put(CMQC.PORT_PROPERTY, new Integer(port));\r\n         mqht.put(CMQC.USER_ID_PROPERTY, params.get(&quot;-u&quot;));\r\n         mqht.put(CMQC.PASSWORD_PROPERTY, params.get(&quot;-x&quot;));\r\n\r\n         \/\/ I don't want to see MQ exceptions at the console.\r\n         MQException.log = null;\r\n      }\r\n      else\r\n      {\r\n         throw new IllegalArgumentException();\r\n      }\r\n   }\r\n   \r\n   \/**\r\n    * Handle connecting to the queue manager, issuing PCF command then \r\n    * looping through PCF response messages and disconnecting from \r\n    * the queue manager. \r\n    *\/\r\n   private void doPCF()\r\n   {\r\n      MQQueueManager qMgr = null;\r\n      PCFMessageAgent agent = null;\r\n      PCFMessage   request = null;\r\n      PCFMessage&#x5B;] responses = null;\r\n      \r\n      try\r\n      {\r\n         qMgr = new MQQueueManager(qMgrName, mqht);\r\n         MQListQueueAttributes01.logger(&quot;successfully connected to &quot;+ qMgrName);\r\n\r\n         agent = new PCFMessageAgent(qMgr);\r\n         MQListQueueAttributes01.logger(&quot;successfully created agent&quot;);\r\n      \r\n         \/\/ https:\/\/www.ibm.com\/support\/knowledgecenter\/en\/SSFKSJ_9.1.0\/com.ibm.mq.ref.adm.doc\/q087800_.htm\r\n         request = new PCFMessage(CMQCFC.MQCMD_INQUIRE_Q);\r\n         \r\n         \/**\r\n          * You can explicitly set a queue name like &quot;TEST.Q1&quot; or\r\n          * use a wild card like &quot;TEST.*&quot;\r\n          *\/\r\n         request.addParameter(CMQC.MQCA_Q_NAME, &quot;*&quot;);\r\n         \r\n         \/\/ Add parameter to request only local queues\r\n         request.addParameter(CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL);\r\n\r\n         \/\/ Add parameter to request all of the attributes of the queue\r\n         request.addParameter(CMQCFC.MQIACF_Q_ATTRS, new int &#x5B;] { CMQCFC.MQIACF_ALL });\r\n\r\n         responses = agent.send(request);\r\n         \r\n         MQListQueueAttributes01.logger(&quot;responses.length=&quot;+responses.length);\r\n         \r\n         for (int i = 0; i &lt; responses.length; i++)\r\n         {\r\n            if ( ((responses&#x5B;i]).getCompCode() == CMQC.MQCC_OK) &amp;&amp;\r\n                 ((responses&#x5B;i]).getParameterValue(CMQC.MQCA_Q_NAME) != null) )\r\n            {\r\n               String name = responses&#x5B;i].getStringParameterValue(CMQC.MQCA_Q_NAME);\r\n               if (name != null)\r\n                  name = name.trim();\r\n\r\n               int depth = responses&#x5B;i].getIntParameterValue(CMQC.MQIA_CURRENT_Q_DEPTH);\r\n               int maxDepth = responses&#x5B;i].getIntParameterValue(CMQC.MQIA_MAX_Q_DEPTH);\r\n               \r\n               \/\/ many, many attributes can be listed - see web page mentioned above.\r\n               \r\n               MQListQueueAttributes01.logger(&quot;Name=&quot;+name + &quot; : depth=&quot;+depth + &quot; : max_depth=&quot;+maxDepth);\r\n            }\r\n         }\r\n      }\r\n      catch (MQException e)\r\n      {\r\n         MQListQueueAttributes01.logger(&quot;CC=&quot; +e.completionCode + &quot; : RC=&quot; + e.reasonCode);\r\n      }\r\n      catch (IOException e)\r\n      {\r\n         MQListQueueAttributes01.logger(&quot;IOException:&quot; +e.getLocalizedMessage());\r\n      }\r\n      catch (MQDataException e)\r\n      {\r\n         MQListQueueAttributes01.logger(&quot;MQDataException:&quot; +e.getLocalizedMessage());\r\n      }\r\n      finally\r\n      {\r\n         try\r\n         {\r\n            if (agent != null)\r\n            {\r\n               agent.disconnect();\r\n               MQListQueueAttributes01.logger(&quot;disconnected from agent&quot;);\r\n            }\r\n         }\r\n         catch (MQDataException e)\r\n         {\r\n            MQListQueueAttributes01.logger(&quot;CC=&quot; +e.completionCode + &quot; : RC=&quot; + e.reasonCode);\r\n         }\r\n\r\n         try\r\n         {\r\n            if (qMgr != null)\r\n            {\r\n               qMgr.disconnect();\r\n               MQListQueueAttributes01.logger(&quot;disconnected from &quot;+ qMgrName);\r\n            }\r\n         }\r\n         catch (MQException e)\r\n         {\r\n            MQListQueueAttributes01.logger(&quot;CC=&quot; +e.completionCode + &quot; : RC=&quot; + e.reasonCode);\r\n         }\r\n      }\r\n   }\r\n\r\n   \/**\r\n    * A simple logger method\r\n    * @param data\r\n    *\/\r\n   public static void logger(String data)\r\n   {\r\n      String className = Thread.currentThread().getStackTrace()&#x5B;2].getClassName();\r\n\r\n      \/\/ Remove the package info.\r\n      if ( (className != null) &amp;&amp; (className.lastIndexOf('.') != -1) )\r\n         className = className.substring(className.lastIndexOf('.')+1);\r\n\r\n      System.out.println(LOGGER_TIMESTAMP.format(new Date())+&quot; &quot;+className+&quot;: &quot;+Thread.currentThread().getStackTrace()&#x5B;2].getMethodName()+&quot;: &quot;+data);\r\n   }\r\n\r\n   public static void main(String&#x5B;] args)\r\n   {\r\n      MQListQueueAttributes01 mqlqs = new MQListQueueAttributes01();\r\n      \r\n      try\r\n      {\r\n         mqlqs.init(args);\r\n         mqlqs.doPCF();\r\n      }\r\n      catch (IllegalArgumentException e)\r\n      {\r\n         MQListQueueAttributes01.logger(&quot;Usage: java MQListQueueAttributes01 -m QueueManagerName -h host -p port -c channel -u UserID -x Password&quot;);\r\n         System.exit(1);\r\n      }\r\n\r\n      System.exit(0);\r\n   }\r\n}<\/pre>\n<p>Regards,<br \/>\nRoger Lacroix<br \/>\nCapitalware Inc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have done the following runmqsc command to display all local queues with attributes of a queue manager: DIS QL(*) ALL And you wanted to do the same thing via a program, here is a fully functioning Java MQ example that will connect to a remote queue manager, issue a PCF &#8220;Inquire Queue&#8221; command, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,63,14,4,5,12,17,18,67,26,10,11],"tags":[],"class_list":["post-5463","post","type-post","status-publish","format-standard","hentry","category-capitalware","category-hpe-nonstop","category-ibm-i-os400","category-mq","category-java","category-linux","category-mac-os-x","category-open-source","category-pcf","category-programming","category-unix","category-windows"],"_links":{"self":[{"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=\/wp\/v2\/posts\/5463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5463"}],"version-history":[{"count":5,"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=\/wp\/v2\/posts\/5463\/revisions"}],"predecessor-version":[{"id":5475,"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=\/wp\/v2\/posts\/5463\/revisions\/5475"}],"wp:attachment":[{"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capitalware.com\/rl_blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}