package com.wcsadmin.utilities.mq; /** * @author Sahadev Komaragiri **/ import com.ibm.mq.*; public class BrowseMessages { private String qManager; private String qName; public static void main(String args[]) { BrowseMessages browseMsgs = new BrowseMessages(); browseMsgs.qManager = args[0]; browseMsgs.qName = args[1]; browseMsgs.runNow(); } public void runNow() { MQQueueManager qMgr = null; MQQueue queue = null; try { // Create a connection to the QueueManager System.out.println("Connecting to queue manager: "+qManager); qMgr = new MQQueueManager(qManager); // Set up the options on the queue we wish to open int openOptions = MQC.MQOO_BROWSE | MQC.MQOO_INPUT_SHARED; // Now specify the queue that we wish to open and the open options System.out.println("Accessing queue: "+qName); queue = qMgr.accessQueue(qName, openOptions,null,null,null); MQGetMessageOptions getMessageOptions = new MQGetMessageOptions(); getMessageOptions.options = MQC.MQGMO_BROWSE_FIRST | MQC.MQGMO_WAIT; getMessageOptions.waitInterval = 1000; // Get the message off the queue. int iCount = 0; while(true) { iCount++; MQMessage rcvMessage = new MQMessage(); queue.get(rcvMessage, getMessageOptions); byte[] b = new byte[rcvMessage.getMessageLength()]; rcvMessage.readFully(b); System.out.println("Message : " + iCount); String msgText = new String(b); if (msgText.trim().equals("")) break; System.out.println(msgText); getMessageOptions.options = MQC.MQGMO_BROWSE_NEXT; } } catch (MQException ex) { int MQRC_NO_MSG_AVAILABLE = 2033; if (ex.reasonCode == MQRC_NO_MSG_AVAILABLE) { System.out.println("No more messages "); } else { System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode + " Reason Code " + ex.reasonCode); } } catch (java.io.IOException ex) { System.out.println("An IOException occured while reading to the message buffer: " + ex); } finally { try { System.out.println("Closing the queue"); queue.close(); System.out.println("Disconnecting from the Queue Manager"); qMgr.disconnect(); System.out.println("Done!"); } catch(Exception e) { System.out.println("Error in finally!"); } } } }