DataQueue Consumer Example


//////////////////////////////////////////////////////////////////////////////////
//
// Data Queue example.  This program uses the Data Queue classes to read
// entries off a data queue on the AS/400.  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:
//    DQConsumer system
//
//////////////////////////////////////////////////////////////////////////////////
//
// This source is an example of AS/400 Toolbox for Java "DataQueue".
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind.  The implied warranties of
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// AS/400 Toolbox for Java
// (C) Copyright IBM Corp. 1997
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
//////////////////////////////////////////////////////////////////////////////////


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

public class DQConsumer 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];



         // 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), "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);


         try
         {
             // Create an AS400 object for the AS/400 that has the data queue.

             AS400 as400 = new AS400(system);



             // Create the data queue object that represents the data queue on
             // the AS/400.

             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 AS/400 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("  DQConsumer system");
         System.out.println("");
         System.out.println("Where");
         System.out.println("");
         System.out.println("  system = AS/400 that has the data queue");
         System.out.println("");
         System.out.println("For example:");
         System.out.println("");
         System.out.println("  DQConsumer mySystem");
         System.out.println("");
         System.out.println("");
      }

      System.exit(0);
   }
}