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

Note: Read the Code example disclaimer for important legal information.
///////////////////////////////////////////////////////////////////////////////
//
// Data Queue example.  This program uses the Data Queue classes to read
// entries off a data queue on the server.  The entries were put on the
// queue with the DQProducer example program.
//
// This is the consumer side of the producer/consumer example.  It reads
// entries off the queue and process them.
//
// Command syntax:
//    DQConsumerExample system
//
///////////////////////////////////////////////////////////////////////////////


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

public class DQConsumerExample 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)
      {
         try
         {

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

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


             // Build a record format for the format of the data queue entry.
             // This format matches the format in the DQProducer class.  A
             // record consists of:
             //    - a four byte number -- the customer number
             //    - a four byte number -- the part number
             //    - a 20 character string -- the part description
             //    - a four byte number -- the number of parts in this order

             // First create the base data types.
             BinaryFieldDescription customerNumber =
                new BinaryFieldDescription(new AS400Bin4(), "CUSTOMER_NUMBER");

             BinaryFieldDescription partNumber =
                new BinaryFieldDescription(new AS400Bin4(), "PART_NUMBER");

             CharacterFieldDescription partName =
                new CharacterFieldDescription(new AS400Text(20, as400), "PART_NAME"

             BinaryFieldDescription quantity =
                new BinaryFieldDescription(new AS400Bin4(), "QUANTITY"

             // Build a record format and fill it with the base data types.
             RecordFormat dataFormat = new RecordFormat();

             dataFormat.addFieldDescription(customerNumber);
             dataFormat.addFieldDescription(partNumber);
             dataFormat.addFieldDescription(partName);
             dataFormat.addFieldDescription(quantity);

             // Create the data queue object that represents the data queue on
             // the server.
             DataQueue dq = new DataQueue(as400, "/QSYS.LIB/JAVADEMO.LIB/PRODCONS.DTAQ");

             boolean Continue = true;

             // Read the first entry off the queue.  The timeout value is
             // set to -1 so this program will wait forever for an entry.
             System.out.println("*** Waiting for an entry for process ***");

             DataQueueEntry DQData = dq.read(-1);

             while (Continue)
             {

                // We just read an entry off the queue.  Put the data into
                // a record object so the program can access the fields of
                // the data by name.  The Record object will also convert
                // the data from server format to Java format.
                Record data = dataFormat.getNewRecord(DQData.getData());

                // Get two values out of the record and display them.
                Integer amountOrdered = (Integer) data.getField("QUANTITY");
                String  partOrdered   = (String)  data.getField("PART_NAME");

                System.out.println("Need " + amountOrdered + " of "
                                   + partOrdered);
                System.out.println(" ");
                System.out.println("*** Waiting for an entry for process ***");

                // Wait for the next entry.
                DQData = dq.read(-1);
             }
         }
         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(" DQConsumerExample 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(" DQConsumerExample mySystem");
         System.out.println("");
         System.out.println("");
      }

      System.exit(0);
   }
}