Message classes

The IBM® Toolbox for Java™ AS400Message class and its associated classes represent a message returned from a server.

AS400Message

The AS400Message object allows the Java program to retrieve an IBM i message that is generated from a previous operation (for example, from a command call). From a message object, the Java program can retrieve the following:

The following example shows how to use the AS400Message object:
Note: Read the Code example disclaimer for important legal information.
     // Create a command call object.
     CommandCall cmd = new CommandCall(sys, "myCommand");

     // Run the command
     cmd.run();

     // Get the list of messages that are
     // the result of the command that I just ran
     AS400Message[] messageList = cmd.getMessageList();

     // Iterate through the list displaying the messages
     for (int i = 0; i < messageList.length; i++)
     {
        System.out.println(messageList[i].getText());
     }

Examples: Using message lists

The following examples show how you can use message lists with CommandCall and ProgramCall.

QueuedMessage

The QueuedMessage class extends the AS400Message class.

The QueuedMessage class accesses information about a message on an IBM i message queue. With this class, a Java program can retrieve:

The following example prints all messages in the message queue of the current (signed-on) user:
Note: Read the Code example disclaimer for important legal information.
      // The message queue is on this system.
      AS400 sys = new AS400(mySystem.myCompany.com);

      // Create the message queue object.
      // This object will represent the
      // queue for the current user.
      MessageQueue queue = new MessageQueue(sys, MessageQueue.CURRENT);

      // Get the list of messages currently in this user's queue.
      Enumeration e = queue.getMessages();

      // Print each message in the queue.
      while (e.hasMoreElements())
      {
         QueuedMessage msg = e.getNextElement();
         System.out.println(msg.getText());
      }

MessageFile

The MessageFile class allows you to receive a message from an IBM i message file. The MessageFile class returns an AS400Message object that contains the message. Using the MessageFile class, you can do the following:

The following example shows how to retrieve and print a message:
Note: Read the Code example disclaimer for important legal information.
     AS400 system = new AS400("mysystem.mycompany.com");
     MessageFile messageFile = new MessageFile(system);
     messageFile.setPath("/QSYS.LIB/QCPFMSG.MSGF");
     AS400Message message = messageFile.getMessage("CPD0170");
     System.out.println(message.getText());

MessageQueue

The MessageQueue class allows a Java program to interact with an IBM i message queue.

The MessageQueue class acts as a container for the QueuedMessage class. The getMessages() method, in particular, returns a list of QueuedMessage objects. The MessageQueue class can do the following:

The following example lists messages in the message queue for the current user:
Note: Read the Code example disclaimer for important legal information.
      // The message queue is on this system.
      AS400 sys = new AS400(mySystem.myCompany.com);

      // Create the message queue object.
      // This object will represent the
      // queue for the current user.
      MessageQueue queue = new MessageQueue(sys, MessageQueue.CURRENT);

      // Get the list of messages currently in this user's queue.
      Enumeration e = queue.getMessages();

      // Print each message in the queue.
      while (e.hasMoreElements())
      {
         QueuedMessage msg = e.getNextElement();
         System.out.println(msg.getText());
      }