This program uses the KeyedDataQueue class to put records on a data queue.
///////////////////////////////////////////////////////////////////////////////
//
// Data Queue example. This program uses the KeyedDataQueue class to put
// records on a data queue.
//
// The key is a number and the data is a Unicode string. This program
// shows one way to convert on int into a byte array and how to convert
// a Java string into a byte array so it can be written to the queue.
//
// This is the producer side of the producer/consumer example. It puts work
// items on the queue for the consumer to process.
//
// Command syntax:
// DQKeyedProducer system
//
///////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import java.net.*;
import com.ibm.as400.access.*;
public class DQKeyedProducer 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];
System.out.println("Priority is a numeric value. The value ranges are:");
System.out.println(" 0 - 49 = low priority");
System.out.println(" 50 - 100 = medium priority");
System.out.println("100 + = high priority");
System.out.println(" ");
try
{
// Create an AS400 object for the server that has the data queue.
AS400 as400 = new AS400(system);
// Use CommandCall to create the library that contains the
// data queue.
CommandCall crtlib = new CommandCall(as400);
crtlib.run("CRTLIB JAVADEMO");
// Create the data queue object.
QSYSObjectPathName name = new QSYSObjectPathName("JAVADEMO", "PRODCON2", "DTAQ");
KeyedDataQueue dq = new KeyedDataQueue(as400, name.getPath());
// 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. The length of the key is four bytes, the length
// of an entry is 96 bytes.
try
{
dq.create(4, 96);
}
catch (Exception e) {};
// Get the data from the user.
System.out.print("Enter message: ");
String message = inputStream.readLine();
System.out.print("Enter priority: ");
int priority = getInt();
// While there is data to put on the queue.
while (priority > 0)
{
// We want to write a java string as the entry to the queue.
// Input the data queue is a byte array, however, so convert
// the string to a byte array.
byte [] byteData = message.getBytes("UnicodeBigUnmarked");
// The key is a number. Input to the data queue is a byte
// array, however, so convert the int to a byte array;
byte [] byteKey = new byte[4];
byteKey[0] = (byte) (priority >>> 24);
byteKey[1] = (byte) (priority >>> 16);
byteKey[2] = (byte) (priority >>> 8);
byteKey[3] = (byte) (priority);
System.out.println("");
System.out.println("Writing record to the server...");
System.out.println("");
// Write the record to the data queue.
dq.write(byteKey, byteData);
// Get the next value from the user.
System.out.print("Enter message: ");
message = inputStream.readLine();
System.out.print("Enter priority: ");
priority = getInt();
}
}
catch (Exception e)
{
// If any of the above operations failed say the data queue
// operation 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(" DQKeyedProducter 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(" DQKeyedProducer 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;
}
}