Chapter 9. Moving the server application to Java 365
We directly export the classes using the VisualAge for Java export function. When we run the
application on the iSeries server, we have to set the CLASSPATH environment variable to
include the IFS file directory where we stored the class files. In Figure 9-23, we export the
classes to the iSeries server using a network drive.
Figure 9-23 Exporting the class files
9.4.5 Implementing the client
This section explains how to build the client class of the RMI application. We create a new
class that is a subclass of javax.swing.JFrame and implements the PartsContainer interface.
This class uses the ToolboxGUI class as re-usable part. For details about the ToolboxGUI
class and the PartsContainer interface, refer to 4.6.5, Reusable GUI part on page 170.
The name of the client program is RMIExample. As shown in Figure 9-24, we create global
variables for the port name and system name and create a new instance of the ItemSubmitter
class, which we call remoteRequestor. We use the remoteRequestor object for all of our RMI
work.
Figure 9-24 Creating the client class
public class RMIExample extends javax.swing.JFrame implements
DatabaseAccessExample.PartsContainer, java.awt.event.WindowListener
{
private DatabaseAccessExample.ToolboxGUI ivjToolboxGUI1 = null;
static java.lang.String port = null;
static java.lang.String systemName;
ItemSubmitter remoteRequestor = new ItemSubmitter();
366 Building Java Applications for the iSeries Server with VisualAge for Java 3.5
The RMIExample main method (Figure 9-25) was created by VisualAge for Java.
Figure 9-25 RMIExample main() method
When the application is run, the main() method instantiates a new instance of the
RMIExample class. We only have to modify it to use the first argument of the parameter list to
set the port variable.
The connectToDB() method shown in Figure 9-26 is executed when the user clicks the
Connect button.
Figure 9-26 RMIExample connectToDB() method
We use the system name from the screen TextField as a parameter to set the systemName
variable. We call the linked() method of the remoteRequestor object, which was instantiated
from the ItemSubmitter class to establish the RMI connection.
The getRecord() method, shown in Figure 9-27, is called when the user clicks the Get Part
button.
public static void main(java.lang.String[] args) {
if ((port = args[0]) != null)
{
try {
RMIExample aRMIExample;
aRMIExample = new RMIExample();
aRMIExample.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
};
});
aRMIExample.show();
java.awt.Insets insets = aRMIExample.getInsets();
aRMIExample.setSize(aRMIExample.getWidth() + insets.left + insets.right,
aRMIExample.getHeight() + insets.top + insets.bottom);
aRMIExample.setVisible(true);
} catch (Throwable exception) {
System.err.println("Exception occurred in main() of javax.swing.JFrame");
exception.printStackTrace(System.out);
}
}
else
{
System.out.println("Please Input port number");
}
}
public void connectToDB(String systemName, String userid, String password) throws
Exception {
this.systemName = systemName;
remoteRequestor.linked(systemName, port);
return;
}
Chapter 9. Moving the server application to Java 367
Figure 9-27 RMIExample getRecord() method
The getRecord() method calls the submit() method of the remoteRequestor object passing
the part number as a parameter. An Item object is returned that contains the details about the
Item (or Part).
If an Item object is returned, the item requested was found in the database. We use the getter
methods provided by the Item object to retrieve the information and display it on the screen.
The populateAllParts() method, shown in Figure 9-28, is called when the user clicks the
Get All Parts button. It calls the submitAll() method of the remoteRequestor object passing
no parameters.
Figure 9-28 RMIExample populateAllParts() method
public String getRecord(String partNo, javax.swing.JTextField partDesc,
javax.swing.JTextField partQty, javax.swing.JTextField partPrice,
javax.swing.JTextField partDate) throws Exception {
Item rtnItem = remoteRequestor.submit(partNo);
if ((rtnItem) != null)
{
partDesc.setText(rtnItem.getItemDesc());
partDate.setText(rtnItem.getItemDate());
partQty.setText((Integer.toString(rtnItem.getItemQuantity())));
partPrice.setText("$" + (rtnItem.getItemPrice()).toString());
}
else {
partDesc.setText("");
partDate.setText("");
partQty.setText("");
partPrice.setText("");
return "Record not found";
}
return "Record found";
}
public void populateAllParts(javax.swing.table.DefaultTableModel defaultTableModel)
throws Exception
{
Item rtnItem = remoteRequestor.submitAll();
if (rtnItem != null) {
for (int i = 0; i < rtnItem.getNumEntries(); i++) {
ItemDetail rtnDetail = rtnItem.getNextEntry(i);
String[] array = new String[5];
array[0] = rtnDetail.getItemId();
array[1] = rtnDetail.getItemDsc();
array[2] = rtnDetail.getItemPrice();
array[3] = rtnDetail.getItemQty();
array[4] = rtnDetail.getItemDate();
defaultTableModel.addRow(array);
}
}
return;
}

Get Building Java Applications for the iSeries Server with VisualAge for Java 3.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.