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

253 lines
7.6 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 KeyedDataQueue" />
<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="dqkeyedproducerexample" />
<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 KeyedDataQueue</title>
</head>
<body id="dqkeyedproducerexample"><a name="dqkeyedproducerexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using KeyedDataQueue</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 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 &gt;= 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 &gt; 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 &gt;&gt;&gt; 24);
byteKey[1] = (byte) (priority &gt;&gt;&gt; 16);
byteKey[2] = (byte) (priority &gt;&gt;&gt; 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 ==&gt;");
}
}
return i;
}
}</pre>
</div>
</div>
</body>
</html>