Example: Using DataQueueDocument
/////////////////////////////////////////////////////////////////////////
//
// Data queue document example. This program demonstrates how to
// use a document that is associated with an AS/400 data queue.
//
// Command syntax:
// DataQueueDocumentExample system read|write
//
/////////////////////////////////////////////////////////////////////////
//
// This source is an example of IBM Toolbox for Java DataQueueDocument.
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind. The implied warranties of
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// IBM Toolbox for Java
// (C) Copyright IBM Corp. 1997, 1998
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
/////////////////////////////////////////////////////////////////////////
import com.ibm.as400.access.*;
import com.ibm.as400.vaccess.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DataQueueDocumentExample
{
private static DataQueueDocument dqDocument;
private static JTextField text;
private static boolean rw;
public static void main (String[] args)
{
// If a system or read|write was not specified, then display
// help text and exit.
if (args.length != 2)
{
System.out.println("Usage: DataQueueDocumentExample system read|write");
return;
}
rw = args[1].equalsIgnoreCase ("read");
String mode = rw ? "Read" : "Write";
try
{
// Create two frames.
JFrame f = new JFrame ("Data queue document example - " + mode);
// Create an error dialog adapter. This will display
// any errors to the user.
ErrorDialogAdapter errorHandler = new ErrorDialogAdapter (f);
// Create a working cursor adapter. This will adjust
// the cursor whenever a data queue is read or written.
WorkingCursorAdapter cursorAdapter = new WorkingCursorAdapter (f);
// Create an AS400 object. The system name was passed
// as the first command line argument.
AS400 system = new AS400 (args[0]);
// Create the data queue path name.
QSYSObjectPathName dqName = new QSYSObjectPathName ("QGPL",
"JAVATALK", "DTAQ");
// Make sure the the data queue exists.
DataQueue dq = new DataQueue (system, dqName.getPath ());
try
{
dq.create (200);
}
catch (Exception e)
{
// Ignore exceptions. Most likely, the data queue
// already exists.
}
// Create a DataQueueDocument object.
dqDocument = new DataQueueDocument (system, dqName.getPath ());
dqDocument.addErrorListener (errorHandler);
dqDocument.addWorkingListener (cursorAdapter);
// Create a text field used to present the document.
text = new JTextField (dqDocument, "", 40);
text.setEditable (! rw);
// When the program runs, we need a way to control when
// the reads and writes take place. We will let the
// use control this with a button.
Button button = new Button (mode);
button.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent event)
{
if (rw)
dqDocument.read ();
else {
dqDocument.write ();
text.setText ("");
}
}
});
// When the the frame closes, exit.
f.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent event)
{
System.exit (0);
}
});
// Layout the frame.
f.getContentPane ().setLayout (new FlowLayout ());
f.getContentPane ().add (text);
f.getContentPane ().add (button);
f.pack ();
f.show ();
}
catch (Exception e)
{
System.out.println ("Error: " + e.getMessage ());
System.exit (0);
}
}
}