{"id":361,"date":"2016-06-06T22:55:37","date_gmt":"2016-06-06T22:55:37","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=361"},"modified":"2016-07-14T21:04:22","modified_gmt":"2016-07-14T21:04:22","slug":"ibm-websphere-mq-manipulations-part-i","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/06\/06\/ibm-websphere-mq-manipulations-part-i\/","title":{"rendered":"IBM Websphere MQ manipulations &#8211; part I"},"content":{"rendered":"<p>Quite some years ago on a project that I was involved with, I was exposed to Websphere MQ.\u00a0 Our needs were actually very basic to put it mildly.\u00a0 We were using it as a secure method for transferring not messages but actually for transferring entire data files.\u00a0 The files were not all that large and the only requirement was that they were sent with certainty.<\/p>\n<p>We did not need to set special message id&#8217;s or any special attributes.\u00a0 The data was almost always the same type and we used a naming convention for the data files.\u00a0 The files were zipped before they were sent and thus the names and other attributes were preserved.<\/p>\n<p>Management didn&#8217;t tell us if this would be difficult or not, they just asked us to get it done.\u00a0 The Internet proved to be an excellent research tool.\u00a0 There are quite a few examples for doing some simple queue manipulations <a href=\"https:\/\/endrasenn.wordpress.com\/2010\/01\/27\/readwrite-to-ibm-mq-sample-java-code\/\">here<\/a>, <a href=\"http:\/\/www.mqseries.net\/phpBB2\/viewtopic.php?t=63769&amp;sid=28610cbd56938045848fb5f6349efb7d\">here<\/a> and <a href=\"http:\/\/www.codeproject.com\/Articles\/14009\/Interfacing-with-IBM-WebSphere-MQ-formally-IBM-MQS\">here<\/a> just to name a few.<\/p>\n<h2>What exactly is Websphere MQ<\/h2>\n<p>The product emphasizes reliability and robustness of message traffic, and ensures that a message should never be lost when MQ is appropriately configured.\u00a0 Websphere MQ is a multi-platform product that allows delivery of messages in either a homogeneous or heterogeneous computing environment.\u00a0 The strength of solution is due to the guaranteed delivery of messages.<\/p>\n<p>Every product either continues to change and transform or it stagnates and becomes irrelevant.\u00a0 During the evolution of the product, IBM has added to the Java classes that can be used to help lighten the load of using their solution in non-trivial ways.<\/p>\n<p>From release to release IBM most likely does a lot of changes to existing classes or adds new ones to support new functionality.\u00a0 I don&#8217;t have a full overview of all the improvements but the one thing that did come up was the new support for header records.\u00a0 I will discuss more about that in a subsequent article about MQ.<\/p>\n<h2>IBM MQ vs JMS<\/h2>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/IBM_WebSphere_MQ#Message-oriented_middleware\">Websphere MQ<\/a> is an example of a message oriented middleware (MOM) system.\u00a0 In this case, it is a middleware which allows for the transmission of message data between systems.\u00a0 In MQ, the two important pieces that are required are the messages and the queues to store them in.\u00a0 IBM has decided to add their MOM system the &#8220;queue manager&#8221; as part of their particular solution, this doesn&#8217;t exist by other solutions.<\/p>\n<p>Java Message System (JMS) was created to allow Java programs to access existing message systems.\u00a0 However, due to network effects, more and more message oriented middleware systems have also implemented JMS specification.\u00a0 Below is just a small sample of implemented systems accessible via JMS.<\/p>\n<ul>\n<li>Apache MQ<\/li>\n<li>Open Message Queue, from Oracle<\/li>\n<li>OpenJMS, from The OpenJMS Group<\/li>\n<li>Solace JMS from Solace Systems<\/li>\n<li>SAP NetWeaver Process Integration<\/li>\n<li>SonicMQ from Aurea Software<\/li>\n<li>SwiftMQ<\/li>\n<li>Tervela<\/li>\n<\/ul>\n<p>A more comprehensive list can be found on the Internet, or at other sites such as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Java_Message_Service#Provider_implementations\">wikipedia<\/a>.<\/p>\n<h2>MQ Examples<\/h2>\n<p>Below is an\u00a0example of dealing with messages and\u00a0 queues. The put example will actually work for either version 6 or version 7 or newer of Websphere.\u00a0 Considering how simple the actual task is they may work without any changes for some time to come.<\/p>\n<h2>MQ Put \/ Get Example<\/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\n\r\npublic class myMQ6 {\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\tprivate MQQueueManager mqQueueManager; \/\/ for QMGR object\r\n\tprivate MQQueue mqQueue; \/\/ for Queue object\r\n\r\n\tpublic myMQ6() {\r\n\t}\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\t\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\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\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\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 putSimpleMessage(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\tmqm.format = MQC.MQFMT_STRING;\r\n\r\n\t\t\t\/\/ the byte array could be contents of a file, but we \r\n\t\t\t\/\/ will keep this simple.\r\n\t\t\tbyte[] bytearray = messageTextToSend.getBytes();\r\n\r\n\t\t\tmqm.write(bytearray);\r\n\r\n\t\t\t\/\/ send it out.\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 mqExp) \r\n\t\t{\r\n\t\t\tdisplayException(mqExp,\"sending message\");\r\n\t\t\tSystem.exit(1);\t\t\r\n\t\t} \r\n\t\tcatch (IOException e) \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\tprivate void getSimpleMessage()   \r\n\t{\r\n\t\tString spot = \"\";\r\n\t\ttry {\r\n\t\t\tMQMessage mqm = new MQMessage();\t\t\r\n\r\n\t\t\t\/\/ read the message\r\n\t\t\tmqQueue.get(mqm);\r\n\t\t\tspot = \"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\tmqm.readFully(binMessage);\r\n\r\n\t\t\tString messagetext = new String(binMessage);\r\n\t\t\tSystem.out.println(\"message='\" + messagetext+ \"'\");\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 \" + spot);\r\n\t\t\tSystem.exit(1);\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void putSimple(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\tputSimpleMessage(\"hello world!\");\r\n\t\tclose();\r\n\t\tdisconnect();\r\n\t}\r\n\r\n\tpublic void getSimple(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\tgetSimpleMessage();\r\n\t\tclose();\r\n\t\tdisconnect();\r\n\t}\r\n\r\n\tpublic static void main(String[] args) \r\n\t{\r\n\t\tmyMQ6 myputter;\r\n\t\tmyputter = new myMQ6();\r\n\t\tmyputter.putSimple(args);\r\n\t\t\r\n\t\tmyputter.getSimple(args);\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>This was all interesting but extremely basic.\u00a0 MQ gets more interesting when you can add some sorts of meta data to the messages themselves or start using the various headers that are supported.<\/p>\n<p>The two IBM version 6 libraries that are needed are com.ibm.mq.jar and connect.jar.<\/p>\n<p>I will discuss this further in part II in a few days.<\/p>\n<h2>Interesting References<\/h2>\n<p><a href=\"http:\/\/www.scribd.com\/doc\/6811160\/Websphere-MQ-Using-Java\">www.scribd.com\/doc\/6811160\/Websphere-MQ-Using-Java<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quite some years ago on a project that I was involved with, I was exposed to Websphere MQ.\u00a0 Our needs were actually very basic to put it mildly.\u00a0 We were using it as a secure method for transferring not messages &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/06\/06\/ibm-websphere-mq-manipulations-part-i\/\">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\/361"}],"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=361"}],"version-history":[{"count":31,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":991,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions\/991"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}