DataQueue Example
//////////////////////////////////////////////////////////////////////////////////
//
// Data Queue example. This program uses the DataQueue class to put
// records on a data queue.
//
// This example uses the Record and Record format classes to put data
// on the queue. String data is converted from unicode to ebcdic
// and numbers are converted from Java to AS/400 format. Since data
// is converted the data queue entries can be read by an AS/400 program
// or a Client Access/400 program as well as another Java program.
//
// This is the producer side of the producer/consumer example. It puts work
// items on the queue for the consumer to process.
//
// Command syntax:
// DQProducer system
//
//////////////////////////////////////////////////////////////////////////////////
//
// This source is an example of AS/400 Toolbox for Java "DataQueues".
// 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 DQProducer extends Object
{
// Create a reader to get input from the user.
static BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);
public static void main(String[] parameters)
{
System.out.println( " " );
// if the 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 DQConsumer 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 library that contains the data queue using CommandCall.
CommandCall crtlib = new CommandCall(as400);
crtlib.run("CRTLIB JAVADEMO");
// Create the data queue object.
DataQueue dq = new DataQueue(as400, "/QSYS.LIB/JAVADEMO.LIB/PRODCONS.DTAQ");
// Create the data queue just in case this is the first time this
// program has run. The queue already exists exception is caught
// and ignored.
try
{
dq.create(96);
}
catch (Exception e) {};
// Get the first field of data from the user.
System.out.print("Enter customer number (or 0 to quit): ");
int customer = getInt();
// While there is data to put on the queue.
while (customer > 0)
{
// Get the rest of the data for this order from the user.
System.out.print("Enter part number: ");
int part = getInt();
System.out.print("Enter quantity: ");
int quantityToOrder = getInt();
String description = "part " + part;
// Create a record based on the record format. The record
// is empty now but will eventually contain the data.
Record data = new Record(dataFormat);
// Set the values we received from the user into the record.
data.setField("CUSTOMER_NUMBER", new Integer(customer));
data.setField("PART_NUMBER", new Integer(part));
data.setField("QUANTITY", new Integer(quantityToOrder));
data.setField("PART_NAME", description);
// Convert the record into a byte array. The byte array is what
// is actually put to the data queue.
byte [] byteData = data.getContents();
System.out.println("");
System.out.println("Writing record to the AS/400 ...");
System.out.println("");
// Write the record to the data queue.
dq.write(byteData);
// Get the next value from the user.
System.out.print("Enter customer number (or 0 to quit): ");
customer = getInt();
}
}
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(" DQProducter 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(" DQProducer mySystem");
System.out.println("");
System.out.println("");
}
System.exit(0);
}
// This is the subroutine that gets a character string from the user
// and converts it into an int.
static int getInt()
{
int i = 0;
boolean Continue = true;
while (Continue)
{
try
{
String s = inputStream.readLine();
i = (new Integer(s)).intValue();
Continue = false;
}
catch (Exception e)
{
System.out.println(e);
System.out.print("Please enter a number ==>");
}
}
return i;
}
}