Example: Using KeyedDataQueue classes to read entries off a data queue

Note: Read the Code example disclaimer for important legal information.
///////////////////////////////////////////////////////////////////////////////
//
// Keyed Data Queue example.  This program uses the KeyedDataQueue classes to 
// read entries off a data queue on the server.  The entries were put on the
// queue with the DQKeyedProducer example program.
//
// The key is a number and the data is a unicode string.  This program
// shows one way to convert the byte array to a Java int and to read
// a byte array and convert it into a Java string.
//
// This is the consumer side of the producer/consumer example.  It reads
// entries off the queue and process them.
//
// Command syntax:
//    DQKeyedConsumer system
//
///////////////////////////////////////////////////////////////////////////////

import java.io.*;
import java.util.*;
import java.net.*;
import com.ibm.as400.access.*;

public class DQKeyedConsumer extends Object
{
   public static void main(String[] parameters)
   {
      System.out.println( " " );

      // if a system name was not specified, display help text and exit.
      if (parameters.length >= 1)
      {

         // The first parameter is the system that contains the data queue.
         String system = parameters[0];

         // Create byte arrays for the priority boundries:
         // 100 +    = high priority
         // 50 - 100 = medium priority
         //  0 -  49 = low priority

         byte [] key0 = new byte[4];
         key0[0] = 0;
         key0[1] = 0;
         key0[2] = 0;
         key0[3] = 0;

         byte [] key50  = new byte[4];
         key50[0] = (byte) (50 >>> 24);
         key50[1] = (byte) (50 >>> 16);
         key50[2] = (byte) (50 >>> 8);
         key50[3] = (byte) (50);

         byte [] key100 = new byte[4];
         key100[0] = (byte) (100 >>> 24);
         key100[1] = (byte) (100 >>> 16);
         key100[2] = (byte) (100 >>> 8);
         key100[3] = (byte) (100);

         try
         {
             // Create an AS400 object for the server that has the data queue.
             AS400 as400 = new AS400(system);

             // Create the data queue object that represents the data queue
             // on the server.

             QSYSObjectPathName name = new QSYSObjectPathName("JAVADEMO",
                                                              "PRODCON2",
                                                              "DTAQ");
             KeyedDataQueue dq = new KeyedDataQueue(as400, name.getPath());
             KeyedDataQueueEntry DQData = null;

             try
             {
                boolean Continue = true;

                // Go until the user ends us.
                while (Continue)
                {
                   // Look for a high priority item on the queue. If one is
                   // found process that item.  Note the peek method does not
                   // remove the item if one is found. Also note the timeout
                   // is 0. If an item is not found we get control back with
                   // a null data queue entry.
                   DQData = dq.read(key100, 0, "GE");

                   if (DQData != null)
                   {
                      processEntry(DQData);
                   }

                   // else no high priority item was found.  Look for a medium
                   // priority item.
                   else
                   {
                      DQData = dq.read(key50, 0, "GE");

                      if (DQData != null)
                      {
                         processEntry(DQData);
                      }

                      // else no medium priority item was found, look for a low
                      // priority item.
                      else
                      {
                         DQData = dq.read(key0, 0, "GE");

                         if (DQData != null)
                         {
                            processEntry(DQData);
                         }

                         else
                         {
                           System.out.println("Nothing to process, will check again in 30 seconds");
                           Thread.sleep(30000);
                         }
                      }
                   }
                }
             }
             catch (Exception e)
             {
                // If any of the above operations failed say the data queue
                // operation failed and output the exception.
                System.out.println("Could not read from the data queue.");
                System.out.println(e);
             };

         }
         catch (Exception e)
         {
             // If any of the above operations failed say the data queue
             // operation failed and output the exception.

             System.out.println("Data Queue operation failed");
             System.out.println(e);
         }
      }

      // Display help text when parameters are incorrect.
      else
      {
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("Parameters are not correct. Command syntax is:");
         System.out.println("");
         System.out.println(" DQKeyedConsumer system");
         System.out.println("");
         System.out.println("Where");
         System.out.println("");
         System.out.println(" system = Server that has the data queue");
         System.out.println("");
         System.out.println("For example:");
         System.out.println("");
         System.out.println("");
         System.out.println(" DQKeyedConsumer mySystem");
         System.out.println("");
         System.out.println("");
      }

      System.exit(0);
   }

   static void processEntry(KeyedDataQueueEntry DQData)
   {
      try
      {
          // The data is a string.  Get the string out of the data queue entry.
          // In the data queue entry the data is a byte array so convert the
          // entry from a byte array into a string.
          String message  = new String(DQData.getData(), "UnicodeBig");

          // The key is a byte array.  Get the key out of the data queue entry
          // and convert it into a number.
          byte [] keyData = DQData.getKey();

          int keyValue = ((keyData[0] & 0xFF) << 24) +
                         ((keyData[1] & 0xFF) << 16) +
                         ((keyData[2] & 0xFF) <<  8) +
                          (keyData[3] & 0xFF);

          // Output the entry.
          System.out.println("Priority: " + keyValue + "   message: " + message);

      }
      catch (Exception e)
      {
         // If any of the above operations failed say the data queue operation
         // failed and output the exception.

         System.out.println("Could not read from the data queue");
         System.out.println(e);
      }
   }
}