{"id":993,"date":"2016-06-10T18:01:04","date_gmt":"2016-06-10T18:01:04","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=993"},"modified":"2016-07-14T21:05:21","modified_gmt":"2016-07-14T21:05:21","slug":"ibm-websphere-mq-manipulations-part-ii","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/06\/10\/ibm-websphere-mq-manipulations-part-ii\/","title":{"rendered":"IBM Websphere MQ manipulations &#8211; part II"},"content":{"rendered":"<p>In my previous article IBM Websphere MQ manipulations &#8211; part I, I provided an\u00a0example on how to put and get messages into the queue.\u00a0 What we were doing was essentially performing a stateless transfer of information between systems.\u00a0 The data itself is important but it is not related to any of the other data transferred and thus perhaps one of the simplest middleware examples.<\/p>\n<p>IBM Websphere has undoubtedly <a href=\"http:\/\/www.ibm.com\/developerworks\/websphere\/library\/techarticles\/1001_xiao\/1001_xiao.html\">changed over time<\/a>, in version 7 they have added properties and tried to make it more compatible with Java Message Service API.\u00a0\u00a0 They also added some additional classes to make it easier to add or manipulate headers for the messages you are sending.<\/p>\n<h1>MQ Version 7<\/h1>\n<h2>A\u00a0message with a RFH2 header<\/h2>\n<pre><code>import java.io.IOException;\r\n\r\nimport com.ibm.mq.MQC;\r\nimport com.ibm.mq.MQEnvironment;\r\nimport com.ibm.mq.MQException;\r\nimport com.ibm.mq.MQMessage;\r\nimport com.ibm.mq.MQQueue;\r\nimport com.ibm.mq.MQQueueManager;\r\nimport com.ibm.mq.headers.MQDataException;\r\nimport com.ibm.mq.headers.MQHeaderIterator;\r\nimport com.ibm.mq.headers.MQHeaderList;\r\nimport com.ibm.mq.headers.MQRFH2;\r\n\r\npublic class myMQ7 {\r\n\r\n\r\n\tString mqHostName;\r\n\tString mqQueueManagerName;\r\n\tString mqQueueChannel;\r\n\tString mqQueueName;\r\n\tint mqQueuePort;\r\n\r\n\tString filenametoput;\r\n\tString inputpath;\r\n\r\n\tprivate MQQueueManager mqQueueManager; \/\/ for QMGR object\r\n\tprivate MQQueue mqQueue; \/\/ for Queue object\r\n\r\n\r\n\tprivate void displayException(MQException ex, String action)\r\n\t{\r\n\t\tSystem.out.println(\"Error while \" + action);\r\n\t\tSystem.out.println(\"QMGR Name : \" + mqQueueManagerName);\r\n\t\tSystem.out.println(\"Queue Name : \" + mqQueueName);\r\n\t\tSystem.out.println(\"CC   : \" + ex.completionCode);\r\n\t\tSystem.out.println(\"RC   : \" + ex.reasonCode);\r\n\t}\r\n\r\n\tpublic void init(String host, String managername, String channel, String queuename, int queueport)\r\n\t{\r\n\t\tmqHostName \t\t\t= host;\r\n\t\tmqQueueManagerName \t= managername;\r\n\t\tmqQueueChannel  \t= channel;\r\n\t\tmqQueueName    \t\t= queuename;\r\n\t\tmqQueuePort     \t= queueport;\r\n\r\n\t\t\/\/ validity checking left off.\r\n\t}\r\n\r\n\tpublic void connect()  \r\n\t{ \r\n\t\ttry {\r\n\t\t\tMQEnvironment.hostname = mqHostName;\r\n\t\t\tMQEnvironment.channel = mqQueueChannel;\r\n\t\t\tMQEnvironment.port = mqQueuePort;\r\n\r\n\t\t\tmqQueueManager = new MQQueueManager(mqQueueManagerName);\r\n\t\t} \r\n\t\tcatch (MQException mqExp) \r\n\t\t{\r\n\t\t\tdisplayException(mqExp,\"doing queue manager connect\");\r\n\t\t\tSystem.exit(1);\r\n\t\t}\r\n\t}\r\n\tpublic void disconnect()  \r\n\t{ \t\t\t\t\t\t\t\t\t\t\t\t\t \r\n\t\ttry {\r\n\t\t\tmqQueueManager.disconnect();\r\n\t\t} \r\n\t\tcatch (MQException mqExp) \r\n\t\t{\r\n\t\t\tdisplayException(mqExp,\"doing queue manager disconnect\");\r\n\t\t\tSystem.exit(1);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void open()\r\n\t{\r\n\t\tint openOption = MQC.MQOO_OUTPUT | MQC.MQOO_INPUT_AS_Q_DEF;\r\n\r\n\t\ttry {\r\n\t\t\tmqQueue = mqQueueManager.accessQueue(mqQueueName, openOption, null, null, null);\r\n\t\t} \r\n\t\tcatch (MQException e) \t\t\r\n\t\t{\r\n\t\t\tdisplayException(e,\"doing queue open\");\r\n\t\t\tSystem.exit(1);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void close()  \r\n\t{\r\n\t\ttry {\r\n\t\t\tmqQueue.close();\r\n\t\t} \r\n\t\tcatch (MQException mqExp) \r\n\t\t{\r\n\t\t\tdisplayException(mqExp,\"closing queue\");\r\n\t\t\tSystem.exit(1);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void putHeaderMessage(String args[])\r\n\t{\r\n\t\tinit(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));\r\n\t\tconnect();\r\n\t\topen();\r\n\t\tputMessage(\"Aw, the poor puddy tat!\");\r\n\t\tclose();\r\n\t\tdisconnect();\r\n\t}\r\n\t\r\n\tpublic void getHeaderMessage(String args[])\r\n\t{\r\n\t\tinit(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));\r\n\t\tconnect();\r\n\t\topen();\r\n\t\tgetMessage(\"favcolor\");\r\n\t\tclose();\r\n\t\tdisconnect();\r\n\t}\r\n\r\n\tprivate void putMessage(String messageTextToSend) \r\n\t{\r\n\t\ttry {\r\n\t\t\t\/\/ create message\r\n\t\t\tMQMessage mqm = new MQMessage();\r\n\t\t\t\/\/mqm.format = MQC.MQFMT_STRING;\t\t\/\/ if you do this, your header becomes part of the message\r\n\t\t\tmqm.format = MQC.MQFMT_RF_HEADER_2;\t\t\/\/ if you do this, your header is known to websphere and \r\n\t\t\t\/\/ you can get properties off message later.\r\n\r\n\t\t\t\/\/\r\n\t\t\t\/\/ do our header stuff\r\n\t\t\t\/\/\r\n\t\t\tMQHeaderList list = new MQHeaderList ();\r\n\r\n\t\t\t\/\/ create a header type MQRFH2\r\n\t\t\tMQRFH2 myrfh2 = new MQRFH2();\r\n\r\n\t\t\t\/\/ add the codebase value to the user folder \r\n\t\t\t\/\/ yes, it is case sensitive.\r\n\r\n\t\t\ttry {\r\n\t\t\t\tmyrfh2.setFieldValue(\"usr\", \"favcolor\", \"yellow\");\r\n\t\t\t} catch (IOException e1) {\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te1.printStackTrace();\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ Add the header to the list of headers (the MQRFH2 I have created) \r\n\t\t\tlist.add(myrfh2);\r\n\r\n\t\t\t\/\/ Add all headers on the list to the message \r\n\t\t\t\/\/ in this case only one(the MQRFH2 I have created) \r\n\t\t\ttry {\r\n\t\t\t\tlist.write(mqm);\r\n\t\t\t} catch (IOException e1) {\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te1.printStackTrace();\r\n\t\t\t}\r\n\r\n\t\t\t\/\/\r\n\t\t\t\/\/ read in our file and add to message\r\n\t\t\t\/\/\r\n\t\t\tbyte[] bytearray = messageTextToSend.getBytes();\r\n\t\t\tmqm.write(bytearray);\r\n\r\n\t\t\t\/\/ send it out.\r\n\t\t\t\/\/dumpMessage(mqm);\r\n\t\t\tmqQueue.put(mqm);\r\n\r\n\t\t\tSystem.out.println(\"Message sent\");\r\n\t\t} \r\n\t\tcatch (MQException ex) \r\n\t\t{\r\n\t\t\tdisplayException(ex,\"sending header message\");\r\n\t\t\tSystem.exit(1);\t\t\r\n\t\t} \r\n\t\tcatch (IOException ex) \r\n\t\t{\r\n\t\t\tSystem.out.println(\"sending message, write byte array error\");\r\n\t\t\tSystem.exit(1);\t\t\r\n\t\t}\r\n\t}\r\n\r\n\r\n\tprivate String getMessage(String propertyname)   \r\n\t{\r\n\t\tString returnMessage = \"\";\r\n\t\tString codeLocation = \"\";\r\n\t\tString propertyvalue = \"\";\r\n\r\n\t\ttry {\r\n\t\t\tMQMessage mqm = new MQMessage();\r\n\r\n\t\t\t\/\/ read the message\r\n\t\t\tmqQueue.get(mqm);\r\n\t\t\tcodeLocation = \"get message length\";\r\n\t\t\tint mLen = mqm.getMessageLength();\r\n\t\t\tSystem.out.println(\"Got message, all \" + mLen + \" bytes\");\r\n\r\n\t\t\t\/\/ get data from the MQMessage object\r\n\t\t\tbyte[] binMessage = new byte[mLen];\r\n\r\n\t\t\tMQHeaderIterator iter = new MQHeaderIterator(mqm);\r\n\t\t\ttry {\r\n\t\t\t\tcodeLocation = \"skip headers\";\r\n\t\t\t\titer.skipHeaders();\r\n\r\n\t\t\t\t\/\/ this is the actual message length without\r\n\t\t\t\t\/\/ headers and stuff.\r\n\t\t\t\tcodeLocation = \"get data length\";\r\n\t\t\t\tbinMessage = new byte[mqm.getDataLength()];\r\n\t\t\t} \r\n\t\t\tcatch (MQDataException e) \r\n\t\t\t{\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t\tcodeLocation = \"do readFully\";\r\n\t\t\tmqm.readFully(binMessage);\r\n\r\n\t\t\t\/\/ see if our property exists on the message\r\n\t\t\ttry {\r\n\t\t\t\tpropertyvalue = (String)mqm.getObjectProperty(propertyname);\r\n\t\t\t}\r\n\t\t\tcatch (MQException mqExp) \r\n\t\t\t{\r\n\t\t\t\tif (mqExp.reasonCode == 2471)\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/ well, guess that property didn't really exist, not that serious of an error\r\n\t\t\t\t\tpropertyvalue = \"not found\";\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tmqExp.printStackTrace();\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tMQHeaderList headersfoundlist = null;\r\n\t\t\ttry {\r\n\t\t\t\theadersfoundlist = new MQHeaderList (mqm);\r\n\r\n\t\t\t\tSystem.out.println(\"headers found in list = \" + headersfoundlist.size());\r\n\t\t\t\tSystem.out.println(\"headers list empty? \" + headersfoundlist.isEmpty());\r\n\r\n\t\t\t\tif (headersfoundlist.size() != 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tpropertyvalue = \"no property found\";\r\n\t\t\t\t\tint idx = headersfoundlist.indexOf(\"MQRFH2\");\r\n\t\t\t\t\tMQRFH2 rfh = (MQRFH2) headersfoundlist.get(idx);\r\n\t\t\t\t\ttry {\r\n\t\t\t\t\t\tpropertyvalue = (String)rfh.getFieldValue(\"usr\",propertyname);\r\n\t\t\t\t\t} \r\n\t\t\t\t\tcatch (IOException e) \r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\t\t\te.printStackTrace();\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} \r\n\t\t\tcatch (MQDataException e1) \r\n\t\t\t{\t\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te1.printStackTrace();\r\n\t\t\t} \r\n\t\t\tcatch (IOException e1) \r\n\t\t\t{\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te1.printStackTrace();\r\n\t\t\t} \r\n\r\n\t\t\tString foundMessage = new String(binMessage);\r\n\t\t\tSystem.out.println(\"message='\" + foundMessage + \"'\");\r\n\t\t\tSystem.out.println(\"property name=\" + propertyname);\r\n\t\t\tSystem.out.println(\"property value=\" + propertyvalue);\r\n\t\t} \r\n\t\tcatch (MQException mqExp) \r\n\t\t{\r\n\t\t\tif (mqExp.reasonCode == 2033)\r\n\t\t\t{\r\n\t\t\t\t\/\/ queue empty, not really an error\r\n\t\t\t\tSystem.out.println(\"no message found\");\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tdisplayException(mqExp,\"reading from queue\");\r\n\t\t\t\tSystem.exit(1);\r\n\t\t\t}\r\n\t\t} \r\n\t\tcatch (IOException e2) \r\n\t\t{ \r\n\t\t\tSystem.out.println(\"IO Exception while \" + codeLocation);\r\n\t\t\tSystem.exit(1);\r\n\t\t}\r\n\r\n\t\treturn returnMessage;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) \r\n\t{\r\n\t\tmyMQ7 myputter;\r\n\t\tmyputter = new myMQ7();\r\n\t\tmyputter.putHeaderMessage(args);\r\n\t\tmyputter.getHeaderMessage(args);\r\n\t}\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my previous article IBM Websphere MQ manipulations &#8211; part I, I provided an\u00a0example on how to put and get messages into the queue.\u00a0 What we were doing was essentially performing a stateless transfer of information between systems.\u00a0 The data &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/06\/10\/ibm-websphere-mq-manipulations-part-ii\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,3],"tags":[12,58],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/993"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=993"}],"version-history":[{"count":2,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/993\/revisions"}],"predecessor-version":[{"id":995,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/993\/revisions\/995"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}