[ Next part | Simple programming examples ]

Example: Using message queues (part 1 of 3)

Use the following as an example for your program. The example includes detailed explanations about key lines in the code. You can view the detailed explanations in the following ways:

Note: Read the Code example disclaimer for important legal information.

//////////////////////////////////////////////////////////////////////////////////
//
// Example using the Message Queue function of the IBM Toolbox for Java
//
// This source is an example of IBM Toolbox for Java "Message Queue".
//
//////////////////////////////////////////////////////////////////////////////////

package examples; Note 1 Click to display a detailed explanation


import java.io.*;
import java.util.*;

import com.ibm.as400.access.*; Note 2 Click to display a detailed explanation

public class displayMessages extends Object
{

   public static void main(String[] parameters) Note 3 Click to display a detailed explanation
   {
      displayMessages me = new displayMessages();
      me.Main(parameters); Note 4 Click to display a detailed explanation

      System.exit(0); Note 5 Click to display a detailed explanation
   } 


   void displayMessage()
   {
   }


   void Main(String[] parms)
   { 
      try Note 6 Click to display a detailed explanation
      {

               // IBM Toolbox for Java code goes here
      }
      catch (Exception e)
      {
         e.printStackTrace(); Note 7 Click to display a detailed explanation
      } 
   }
}
  1. This class is in the 'examples' package. Java uses packages to avoid name conflicts between Java class files.

  2. This line makes all of the IBM Toolbox for Java classes in the access package available to this program. The classes in the access package have the common prefix com.ibm.as400. By using an import statement, the program can refer to a class using just its name, not its fully-qualified name. For example, you can reference the AS400 class by using AS400, instead of com.ibm.as400.AS400.

  3. This class has a main method; therefore, it can be run as an application. To invoke the program, you run java examples.displayMessages. Note that case must match when running the program. Because an IBM Toolbox for Java class is used, jt400.zip must be in the classpath environment variable.

  4. The main method mentioned in Note 3 is static. One of the restrictions of static methods is that static methods can call only other static methods in their class. To avoid this restriction, many java programs create an object, and then do initialization processing in a method called Main. The Main() method can call any other method in the displayMessages object.

  5. The IBM Toolbox for Java creates threads on behalf of the application to carry out IBM Toolbox for Java activity. If the program does not issue System.exit(0) at termination time, the program may not terminate normally. For example, suppose this program was run from a Windows 95 DOS prompt. Without this line, the command prompt would not return when the program finished. The user would have to enter Ctrl-C to get a command prompt.

  6. The IBM Toolbox for Java code throws exceptions that your program must catch.

  7. This program displays the text of the exception while the program is performing error processing. Exceptions thrown by the IBM Toolbox for Java are translated, so the text of the exception will be the same as the language of the workstation.

[ Next part | Simple programming examples ]