/*****************************************************************************/ /* */ /* (c) Copyright IBM Corp. 2004 All rights reserved. */ /* */ /* This sample program is owned by International Business Machines */ /* Corporation or one of its subsidiaries ("IBM") and is copyrighted */ /* and licensed, not sold. */ /* */ /* You may copy, modify, and distribute this sample program in any */ /* form without payment to IBM, for any purpose including developing, */ /* using, marketing or distributing programs that include or are */ /* derivative works of the sample program. */ /* */ /* The sample program is provided to you on an "AS IS" basis, without */ /* warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, */ /* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* Some jurisdictions do not allow for the exclusion or limitation of */ /* implied warranties, so the above limitations or exclusions may not */ /* apply to you. IBM shall not be liable for any damages you suffer as */ /* a result of using, modifying or distributing the sample program or */ /* its derivatives. */ /* */ /*****************************************************************************/ /* */ /* Program name: putGroup.java */ /* */ /* Description: Sample Java program that demonstrates putting messages that */ /* are in a message group */ /* */ /*****************************************************************************/ /* */ /* Function: */ /* */ /* This program is similar to the putgroup.c program that can be found on: */ /* */ /* http://www.developer.ibm.com/tech/sampmq.html */ /* */ /* This program will put messages onto the queue that are part of a group. */ /* This is done by looking for the enter key (blank line) being used at */ /* the end of each group. Because of this, a second enter (blank line) is */ /* needed to end the program. */ /* */ /* For example, the following would be sample input: */ /* */ /* text entered comment */ /* ----------------------------- ------------------------------- */ /* 1st message group 1st message of group 1 */ /* group 1.2 2nd message of group 1 */ /* group 1.3 3rd message of group 1 */ /* indicate group 1 is finished */ /* 2nd message group 1st message of group 2 */ /* group 2.2 2nd message of group 2 */ /* group 2.3 3rd message of group 2 */ /* indicate group 2 is finished */ /* 3rd message group 1st message of group 3 */ /* group 3.2 2nd message of group 3 */ /* indicate group 3 is finished */ /* indicate we're done */ /* */ /* The program will generate an extra message with the enter key that */ /* ends the group that will be used to mark the end of the group. */ /* */ /*****************************************************************************/ /* */ /* This program is run as follows: */ /* */ /* java putGroup -q ... -qm ... */ /* */ /* where */ /* -q is the queue name */ /* -qm is the queue manager name */ /* */ /* An alternate method is to run the program with an putGroup.properties */ /* that contains the q, qm: */ /* */ /* q=SYSTEM.DEFAULT.LOCAL.QUEUE */ /* qm=pubsub.qmgr */ /* */ /* This program has been tested with WebSphere MQ 5.3 CSD5 and JDK 1.4.1. */ /* */ /* This program can be run with the putgroup sample which will put messages */ /* onto the queue in a group. */ /* */ /*****************************************************************************/ import java.io.*; import java.lang.*; import com.ibm.mq.*; import java.util.Properties; public class putGroup implements Runnable { private static String iQueue = null; private static String iQmgr = null; public putGroup() { iQueue = System.getProperty("q"); iQmgr = System.getProperty("qm"); /***********************************************/ /* Now go and get the putGroup.properties file */ /***********************************************/ try { Properties props = new Properties(System.getProperties()); props.load( new BufferedInputStream( new FileInputStream("putGroup.properties"))); System.setProperties(props); } catch (Exception e) { System.out.println("Unable to read putGroup.properties:"); System.out.println(" " + e); } if (iQueue == null) { iQueue = System.getProperty("q"); } if (iQmgr == null) { iQmgr = System.getProperty("qm"); } } public putGroup(String[] args) { this(); /**********************************/ /* Get the command-line arguments */ /**********************************/ for (int i = 0; i < args.length; i++) { String arg = args[i].toLowerCase(); if (arg.equals("-q")) { if (i + 1 < args.length) { iQueue = args[++i]; } else { System.out.println("didn't specify queue, exiting"); return; } } else if (arg.equals("-qm")) { if (i + 1 < args.length) { iQmgr = args[++i]; } else { System.out.println("didn't specify queue manager, exiting"); return; } } else { System.out.println("Unknown argument: " + arg); } } } public void run() { MQQueueManager qMgr; boolean doneWithGet = false; System.out.println("putGroup started...."); MQException.log = null; try { if (iQmgr != null) { qMgr = new MQQueueManager(iQmgr); } else { qMgr = new MQQueueManager(""); } try { int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING; MQQueue myQueue = qMgr.accessQueue(iQueue, openOptions, null, null, null); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String userInput = null; do { try { userInput = br.readLine(); if (userInput.length() > 0) { MQMessage myMessage = new MQMessage(); myMessage.messageId = null; myMessage.correlationId = null; myMessage.groupId = null; myMessage.writeString(userInput); myMessage.format = MQC.MQFMT_STRING; myMessage.messageFlags = MQC.MQMF_MSG_IN_GROUP; MQPutMessageOptions pmo = new MQPutMessageOptions(); pmo.options = MQC.MQPMO_LOGICAL_ORDER | MQC.MQPMO_SYNCPOINT; myQueue.put(myMessage, pmo); System.out.print("Group ID: "); dumpHexId(myMessage.groupId); System.out.println( "-->Hit enter when done with messages for group"); do { userInput = br.readLine(); if (userInput.length() > 0) { myMessage.clearMessage(); myMessage.writeString(userInput); myMessage.messageFlags = MQC.MQMF_MSG_IN_GROUP; myQueue.put(myMessage, pmo); } } while (userInput.length() > 0); myMessage.messageFlags = MQC.MQMF_LAST_MSG_IN_GROUP; userInput = "Program generated last group message"; myMessage.clearMessage(); myMessage.writeString(userInput); myQueue.put(myMessage, pmo); qMgr.commit(); System.out.println("-->Group now finished"); } } catch (MQException ex) { System.err.println( "Error putting message onto queue " + ex.completionCode + " " + ex.reasonCode); } catch (IOException ex) { System.err.println("IOException"); } } while (userInput.length() > 0); myQueue.close(); } catch (MQException ex) { System.err.println( "Error opening queue " + ex.completionCode + " " + ex.reasonCode); } qMgr.disconnect(); } catch (MQException ex) { System.err.println( "Error opening queue manager " + ex.completionCode + " " + ex.reasonCode); } System.out.println("putGroup finished...."); } /****************************************************/ /* Some of the MQ Ids are actually byte strings and */ /* need to be dumped in hex format. */ /****************************************************/ static void dumpHexId(byte[] myId) { System.out.print("X'"); for (int i = 0; i < myId.length; i++) { char b = (char) (myId[i] & 0xFF); if (b < 0x10) { System.out.print("0"); } System.out.print((String) (Integer.toHexString(b)).toUpperCase()); } System.out.println("'"); } public static void main(String[] args) { putGroup app = new putGroup(args); if (iQueue == null) { System.out.println("Usage:"); System.out.println("java putGroup -q ... -qm ..."); System.out.println("where -q is the name of the queue"); System.out.println( " -qm is the name of the queue manager (optional)"); } else { Thread t = new Thread(app); t.start(); } } }