DataQueue Keyed Consumer Example
//////////////////////////////////////////////////////////////////////////////////
//
// Keyed Data Queue example. This program uses the KeyedDataQueue classes to read
// entries off a data queue on the AS/400. 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
//
//////////////////////////////////////////////////////////////////////////////////
//
// 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 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 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.
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. 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 = AS/400 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);
}
}
}