ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/dqproducerexample.htm

229 lines
8.9 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Using DataQueue classes to put data on a queue" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="dqproducerexample" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Using DataQueue classes to put data on a queue</title>
</head>
<body id="dqproducerexample"><a name="dqproducerexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using DataQueue classes to put data on a queue</h1>
<div><p></p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm#codedisclaimer">Code
example disclaimer</a> for important legal information.</div>
<pre>///////////////////////////////////////////////////////////////////////////////
//
// 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 the server format. Since data
// is converted the data queue entries can be read by a server program
// or a iSeries Access for Windows 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:
// DQProducerExample system
//
///////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import java.net.*;
import com.ibm.as400.access.*;
public class DQProducerExample 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 &gt;= 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 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, 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 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 &gt; 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 server ...");
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 = Server that has the data queue");
System.out.println("");
System.out.println("For example:");
System.out.println("");
System.out.println(" DQProducerExample 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 ==&gt;");
}
}
return i;
}
}</pre>
</div>
</div>
</body>
</html>