FieldDescription/RecordFormat/Record example usage
The following examples build on each other to show how the FieldDescription,
RecordFormat,
and Record classes could be used with the data queue class.
FieldDescriptions
The FieldDescription classes can be used to describe the different types of data that make up an entry on a data queue.
For this example we will assume the following format for the entries on the data queue:
|
Message number Sender Time sent Message text Reply required
| | | | |
bin(4) char(50) char(8) char(1024) char(1)
// Create field descriptions for the entry data
BinaryFieldDescription msgNumber = new BinaryFieldDescription(new AS400Bin4(), "msgnum");
CharacterFieldDescription sender = new CharacterFieldDescription(new AS400Text(50), "sender");
CharacterFieldDescription timeSent = new CharacterFieldDescription(new AS400Text(8), "timesent");
CharacterFieldDescription msgText = new CharacterFieldDescription(new AS400Text(1024), "msgtext");
CharacterFieldDescription replyRequired = new CharacterFieldDescription(new AS400Text(1), "replyreq");
RecordFormat
The RecordFormat class can be used to describe the data that makes up the data queue entry.
The format of the entry can be described and used dynamically in a Java programas shown below:
|
RecordFormat entryFormat = new RecordFormat();
// Describe the fields in an entry on the data queue
entryFormat.addFieldDescription(msgNumber);
entryFormat.addFieldDescription(sender);
entryFormat.addFieldDescription(timeSent);
entryFormat.addFieldDescription(msgText);
entryFormat.addFieldDescription(replyRequired);
// Get a record based on the format of the entries on the data queue
Record rec = entryFormat.getNewRecord();
Or the record format can be defined statically as shown below. Defining the record format
statically allows the format to be used by many programs without coding
the record format multiple times.
|
public class MessageEntryFormat extends RecordFormat
{
// The field descriptions are contained in the class
static BinaryFieldDescription msgNumber = new BinaryFieldDescription(new AS400Bin4(), "msgnum");
static CharacterFieldDescription sender = new CharacterFieldDescription(new AS400Text(50), "sender");
static CharacterFieldDescription timeSent = new CharacterFieldDescription(new AS400Text(8), "timesent");
static CharacterFieldDescription msgText = new CharacterFieldDescription(new AS400Text(1024), "msgtext");
static CharacterFieldDescription replyRequired = new CharacterFieldDescription(new AS400Text(1), "replyreq");
public MessageEntryFormat()
{
// We will name this format for posterity
super("MessageEntryFormat");
// Add the field descriptions
addFieldDescription(msgNumber);
addFieldDescription(sender);
addFieldDescription(timeSent);
addFieldDescription(msgText);
addFieldDescription(replyRequired);
}
}
The statically defined RecordFormat can be used by a Java program as follows:
|
MessageEntryFormat entryFormat = new MessageEntryFormat();
// Get a record based on the format of the entries on the data queue
Record rec = entryFormat.getNewRecord();
Record
The record class can be used to access the individual fields of the data queue entry.
A generic Record object can be used:
|
// Instantiate our data queue object
DataQueue dq = new DataQueue(new AS400(), "/qsys.lib/mylib.lib/myq.dtaq");
// Read an entry
DataQueueEntry dqEntry = null;
try
{
dqEntry = dq.read();
}
catch(Exception e)
{
// Handle any exceptions
}
// Get a record object from our record format, initializing it with the data from the entry we
// just read.
Record rec = entryFormat.getNewRecord(dqEntry.getByteData());
// Output the complete entry as a String. The contents of the record are converted to Java Objects
// based on the record format of the entry.
System.out.println(rec.toString());
// Get the contents of the individual fields of the entry. Each field's contents are converted to
// a Java Object. Field
Integer num = (Integer)rec.getField(0); // Retrieve contents by index
String s = (String)rec.getField("sender");// Retrieve contents by field name
String text = (String)rec.getField(3); // Retrieve the message text
// Output the data
System.out.println(num + " " + s + " " + text);
Or a Record specific to the format of this data queue can be defined statically and used. This allows
getters and setters to be provided for the fields which are more meaningfully named than getField() and
setField(). Also it allows us to return basic Java types instead of Objects if we wish. We are also
able to let the user know the return type to be expected. Note that in the example above we had to
explicitly cast to the correct Java Object.
|
public class MessageEntryRecord extends Record
{
static private RecordFormat format = new MessageEntryFormat();
public MessageEntryRecord()
{
super(format);
}
public int getMessageNumber()
{
// Return the message number as an int. Note: We know our record format and therefore
// know the names of our fields. It is safest to get the field by name in case a field
// has been inserted into the format unbeknownst to us.
return ((Integer)getField("msgnum")).intValue();
}
public String getMessageText()
{
// Return the text of the message
return (String)getField("msgtext");
}
public String getSender()
{
// Return the sender of the message
return (String)getField("sender");
}
public String getTimeSent()
{
// Return the sender of the message
return (String)getField("timesent");
}
// We could add setters here
}
Now we can use our MessageEntryRecord to interpret the data queue entry:
|
// Get a record object from our record format, initializing it with the data from the entry we
// just read.
MessageEntryRecord rec = (MessageEntryRecord)entryFormat.getNewRecord(dqEntry.getByteData());
// Output the complete entry as a String. The contents of the record are converted to Java Objects
// based on the record format of the entry.
System.out.println(rec.toString());
// Get the contents of the individual fields of the entry. Each field's contents are converted to
// a Java Object.
int num = rec.getMessageNumber(); // Retrieve the message number as an int
String s = rec.getSender(); // Retrieve the sender
String text = rec.getMessageText(); // Retrieve the message text
// Output the data
System.out.println(num + " " + s + " " + text);