The IBM® Toolbox for Java™ AS400Message class and its associated classes represent a message returned from a server.
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:
// 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());
}
The following examples show how you can use message lists with CommandCall and ProgramCall.
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 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());
}
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:
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());
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 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());
}