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

236 lines
8.8 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?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 classes to read entries off a data 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="dqkeyedconsumerexample" />
<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 classes to read entries off a data queue</title>
</head>
<body id="dqkeyedconsumerexample"><a name="dqkeyedconsumerexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using KeyedDataQueue classes to read entries off a data 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>///////////////////////////////////////////////////////////////////////////////
//
// Keyed Data Queue example. This program uses the KeyedDataQueue classes to
// read entries off a data queue on the server. 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
//
///////////////////////////////////////////////////////////////////////////////
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 &gt;= 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 &gt;&gt;&gt; 24);
key50[1] = (byte) (50 &gt;&gt;&gt; 16);
key50[2] = (byte) (50 &gt;&gt;&gt; 8);
key50[3] = (byte) (50);
byte [] key100 = new byte[4];
key100[0] = (byte) (100 &gt;&gt;&gt; 24);
key100[1] = (byte) (100 &gt;&gt;&gt; 16);
key100[2] = (byte) (100 &gt;&gt;&gt; 8);
key100[3] = (byte) (100);
try
{
// Create an AS400 object for the server that has the data queue.
AS400 as400 = new AS400(system);
// Create the data queue object that represents the data queue
// on the server.
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. Note the peek method does not
// remove the item if one is found. 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 = Server 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] &amp; 0xFF) &lt;&lt; 24) +
((keyData[1] &amp; 0xFF) &lt;&lt; 16) +
((keyData[2] &amp; 0xFF) &lt;&lt; 8) +
(keyData[3] &amp; 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);
}
}
}</pre>
</div>
</div>
</body>
</html>