IBM MQ系列编程4--------浏览消息

import java.lang.*;
import java.io.*;
import java.util.Properties;
/* import java.util.ResourceBundle; */ /* uncomment if using ResourceBundle */
                                       /* to get program's input            */
import java.util.Calendar;
import com.ibm.mq.*;

public class MQBrowser {
    /****************************/
    /* main program entry point */
    /****************************/
    public static void main( String[] args )     {
      new MQBrowser().runBrowser(args);
    }

  private String qName;
   /********************************************************/
   /* This method does the actually browsing of the queue. */
   /********************************************************/
   public void runBrowser(String[] args) {
      MQQueueManager qMgr        = null;
      MQQueue        browseQueue = null;
      int j = 0; /* used as a message counter */
      if (args.length != 1) {
System.out.println("\nmust have one argument only: queueName\n");
System.exit(0);
      }
      qName = args[0];
      System.out.println("\nMQBrowser.java - starts here");
      System.out.println("**************************");
      MQException.log = null; /* don't print out any exceptions */
      try {
         /*****************************/
         /* Create a queue manager    */
         /*****************************/
// Create a connection to the QueueManager
System.out.println("Connecting to queue manager: "+qManager);
MQEnvironment.channel = System.getProperty ("message.chanel.name");
MQEnvironment.hostname = System.getProperty ("message.queue.server");
MQEnvironment.port = new Integer ((System.getProperty ("message.queue.port"))).intValue();
MQEnvironment.CCSID = new Integer ((System.getProperty ("message.queue.ccsid"))).intValue();
String queueManager= System.getProperty ("message.queue.manager");
qMgr = new MQQueueManager(queueManager);

         /****************************************/
         /* Open the queue in browse mode.       */
         /****************************************/
         int openOptions = MQC.MQOO_BROWSE;
         browseQueue = qMgr.accessQueue(qName, openOptions, null, null, null);
         System.out.println("\n OPEN - '" + qName + "'\n\n");

         /*****************************************************************/
         /* Loop through the queue browsing the messages that are on it.  */
         /*****************************************************************/
         MQGetMessageOptions gmo = new MQGetMessageOptions();
         gmo.options = gmo.options + MQC.MQGMO_BROWSE_NEXT;
         MQMessage myMessage = new MQMessage();
         while (true) {
            myMessage.clearMessage();
            myMessage.correlationId = MQC.MQCI_NONE;
            myMessage.messageId     = MQC.MQMI_NONE;
            browseQueue.get(myMessage, gmo);
            j = j + 1;
            System.out.println(" GET of message number " + j);
            System.out.println("****Message descriptor****\n");
            System.out.println("  StrucId  : 'MD  '"   
                             + "  Version : " + myMessage.getVersion());
            System.out.println("  Report   : " + myMessage.report
                             + "  MsgType : " + myMessage.messageType);
            System.out.println("  Expiry   : " + myMessage.expiry
                             + "  Feedback : " + myMessage.feedback);
            System.out.println("  Encoding : " + myMessage.encoding
                             + "  CodedCharSetId : " + myMessage.characterSet);
            System.out.println("  Format : '" + myMessage.format + "'");
            System.out.println("  Priority : " + myMessage.priority
                             + "  Persistence : " + myMessage.persistence);
            System.out.print("  MsgId : ");
            dumpHexId(myMessage.messageId);
            System.out.print("  CorrelId : ");
            dumpHexId(myMessage.correlationId);
            System.out.println("  BackoutCount : " + myMessage.backoutCount);
            System.out.println("  ReplyToQ     : '"
                             + myMessage.replyToQueueName + "'");
            System.out.println("  ReplyToQMgr  : '"
                             + myMessage.replyToQueueManagerName + "'");

            System.out.println("  ** Identity Context");
            System.out.println("  UserIdentifier : '" + myMessage.userId + "'");
            System.out.println("  Accounting Token :");
            System.out.print("   ");
            dumpHexId(myMessage.accountingToken);
            System.out.println("  ApplIdentityData : '"
                                + myMessage.applicationIdData + "'");
       
            System.out.println("  ** Origin Context");
            System.out.println("  PutApplType    : '"
                               + myMessage.putApplicationType + "'");
            System.out.println("  PutApplName    : '"
                               + myMessage.putApplicationName + "'");

            System.out.print("  PutDate  : '");
            System.out.print(myMessage.putDateTime.get(Calendar.YEAR));
            int myMonth = myMessage.putDateTime.get(Calendar.MONTH) + 1;
            if (myMonth < 10) {System.out.print("0");}
            System.out.print(myMonth);

            int myDay = myMessage.putDateTime.get(Calendar.DAY_OF_MONTH);
            if (myDay < 10) {System.out.print("0");}
            System.out.print(myDay);
            System.out.print("'    ");

            System.out.print("PutTime  : '");
            int myHour = myMessage.putDateTime.get(Calendar.HOUR_OF_DAY);
            if (myHour < 10) { System.out.print("0"); }
            System.out.print(myHour);

            int myMinute = myMessage.putDateTime.get(Calendar.MINUTE);
            if (myMinute < 10) { System.out.print("0"); }
            System.out.print(myMinute);

            int mySecond = myMessage.putDateTime.get(Calendar.SECOND);
            if (mySecond < 10) { System.out.print("0"); }
            System.out.print(mySecond);

            int myMsec = myMessage.putDateTime.get(Calendar.MILLISECOND);
            myMsec = myMsec/10;
            if (myMsec < 10) { System.out.print("0"); }
            System.out.print(myMsec);
            System.out.println("'");

            System.out.println("  ApplOriginData : '"
                               + myMessage.applicationOriginData + "'");
            System.out.println();
            System.out.print("  GroupId : ");
            dumpHexId(myMessage.groupId);
            System.out.println("  MsgSeqNumber   : '"
                               + myMessage.messageSequenceNumber + "'");
            System.out.println("  Offset         : '" + myMessage.offset + "'");
            System.out.println("  MsgFlags       : '"
                               + myMessage.messageFlags + "'");
            System.out.println("  OriginalLength : '"
                               + myMessage.originalLength + "'");
            System.out.println();

            System.out.println("****   Message     ****");
            System.out.println();
            System.out.println(" length - " + myMessage.getMessageLength()
                             + " bytes\n");
            dumpHexMessage(myMessage);
            System.out.println();
            System.out.println();

         }

      } catch (MQException ex) {
         if (ex.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
            System.out.println(" No more messages");
         } else {
            System.out.println("MQ error: Completion code " +
                      ex.completionCode + " Reason code " + ex.reasonCode);
         }
      } catch (java.io.IOException ex) {
         System.out.println("An IO error occurred: " + ex);
      }
       
      try {
         browseQueue.close();
         System.out.println(" CLOSE of queue");
         qMgr.disconnect();
         System.out.println(" DISCONNECT from queue manager");
      } catch (MQException ex) {
         System.out.println("MQ error: Completion code " +
                      ex.completionCode + " Reason code " + ex.reasonCode);
      }

      System.out.println("**************************");
      System.out.println("MQBrowser.java finished");

   }

   /*************************************************************/
   /* This method will dump the text of the message in both hex */
   /* and character format.                                     */
   /*************************************************************/
   static void dumpHexMessage(MQMessage myMsg) throws java.io.IOException {
      
      int DataLength = myMsg.getMessageLength();
      int ch      = 0;
      int chars_this_line = 0;
      int CHARS_PER_LINE  = 16;
      StringBuffer line_text = new StringBuffer();
      line_text.setLength(0);
      do {
         chars_this_line = 0;
         String myPos = new String(Integer.toHexString(ch));
         for (int i=0; i < 8 - myPos.length(); i++) {
            System.out.print("0");
         }
         System.out.print((String)(Integer.toHexString(ch)).toUpperCase() + ": ");

         while ( (chars_this_line < CHARS_PER_LINE) &&
                 (ch < DataLength) ) {

             if (chars_this_line % 2 == 0) {
                System.out.print(" ");
             }
             char b = (char)(myMsg.readUnsignedByte() & 0xFF);
             if (b < 0x10) {
                System.out.print("0");
             }

             System.out.print((String)(Integer.toHexString(b)).toUpperCase());

             /***********************************************/
             /* If this is a printable character, print it. */
             /* Otherwise, print a '.' as a placeholder.    */
             /***********************************************/
             if (Character.isLetterOrDigit(b)
                 || Character.getType(b) == Character.CONNECTOR_PUNCTUATION
                 || Character.getType(b) == Character.CURRENCY_SYMBOL
                 || Character.getType(b) == Character.MATH_SYMBOL
                 || Character.getType(b) == Character.MODIFIER_SYMBOL
                 || Character.getType(b) == Character.UPPERCASE_LETTER
                 || Character.getType(b) == Character.SPACE_SEPARATOR
                 || Character.getType(b) == Character.DASH_PUNCTUATION
                 || Character.getType(b) == Character.START_PUNCTUATION
                 || Character.getType(b) == Character.END_PUNCTUATION
                 || Character.getType(b) == Character.OTHER_PUNCTUATION ) {
                line_text.append(b);
             } else {
                line_text.append('.');
             }
             chars_this_line++;
             ch++;
         }

         /*****************************************************/
         /* pad with blanks to format the last line correctly */
         /*****************************************************/
         if (chars_this_line < CHARS_PER_LINE) {
           for ( ;chars_this_line < CHARS_PER_LINE; chars_this_line++) {
              if (chars_this_line % 2 == 0) System.out.print(" ");
              System.out.print("  ");
              line_text.append(' ');
           }
         }

         System.out.println(" '" + line_text.toString() + "'");
         line_text.setLength(0);
       } while (ch < DataLength);

   } /* end of dumpHexMessage */

   /****************************************************/
   /* 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("'");
   }
}





你可能感兴趣的:(编程,IBM,J#)