Message classes

AS400Message

AS400Message object allows the Java™ program to retrieve an i5/OS™ 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.

Note: Toolbox for Java also provides resource classes that present a generic framework and consistent programming interface for working with various iSeries objects and lists. After reading about the classes in the access package and the resource package, you can choose the object that works best for your application. The resource class for working with queued messages is RQueuedMessage.

The QueuedMessage class accesses information about a message on an iSeries 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 iSeries.
      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 iSeries 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 iSeries message queue.

Note: Toolbox for Java also provides resource classes that present a generic framework and consistent programming interface for working with various iSeries objects and lists. After reading about the classes in the access package and the resource package, you can choose the object that works best for your application. The resource class for working with message queues is RMessageQueue.

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 iSeries.
      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());
      }