Example: Using message queues (part 3 of 3)

[ Previous part ]

Use the following as an example for your program.

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;


import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;

public class displayMessages extends Object
{

   public static void main(String[] parameters)
   {
      displayMessages me = new displayMessages();

      me.Main(parameters);

      System.exit(0);
   }


   void displayMessage()
   {
   }


   void Main(String[] parms)
   {
      try
      {
         AS400 system = new AS400();

         if (parms.length > 0)
            system.setSystemName(parms[0]);

         MessageQueue queue = new MessageQueue(system, MessageQueue.CURRENT);  Note 1 


                  Enumeration e = queue.getMessages();  Note 2 

                  while (e.hasMoreElements())
                  {

                      QueuedMessage message = (QueuedMessage) e.nextElement();  Note 3 
                      System.out.println(message.getText());  Note 4 
                   }
              }
              catch (Exception e)
              {
                 e.printStackTrace();
              }
         }
}
  1. The purpose of this program is to display messages in a server message queue. The MessageQueue object of the IBM® Toolbox for Java™ is used for this task. When the message queue object is constructed, the parameters are the AS400 object and the message queue name. The AS400 object indicates which server contains the resource, and the message queue name identifies which message queue on the server. In this case, a constant is used, which tells the message queue object to access the queue of the signed-on user.
  2. The message queue object gets a list of messages from the server. A connection to the server is made at this point.
  3. Remove a message from the list. The message is in the IBM Toolbox for Java program's QueuedMessage object.
  4. Print the text of the message.

[ Previous part ]