Example 6-34: Utility to test enqueuing and dequeuing messages

import java.sql.*;
import oracle.AQ.*;
import Examples;
import java.io.*;
import oracle.xml.parser.v2.*;
import XMLQueue;
import XMLQueueEmptyException;

public class AQ {
  // Fun little utility to test enqueuing and dequeing XML "Orders"
  public static void main(String[] args) throws Exception {
    Connection conn = Examples.getConnection();
    if (args.length > 0) {
      // Bind to the queue we want to work with
      XMLQueue xmlq = new XMLQueue(conn,"xmlbook","xml_msg_queue");
      // If user wants to enqueue a message
      if (args[0].startsWith("nq")) {
        int msgs = 0;
        for (int argpos=1; argpos < args.length; argpos++) {
          msgs++;
          String id = args[argpos];
          // Create a little <order> XML datagram (very little!)
          String xml ="<order id='"+id+"'></order>";
          // Parse the message into an XMLDocument
          XMLDocument xmldoc = XMLHelper.parse(xml,null);
          // Enqueue the XML message
          xmlq.enqueue(xmldoc);
          System.out.println("Successfully enqueued order# "+id);
        }
        System.out.println("Enqueued "+msgs+" new messages");
      }
      // If user wants to dequeue a message
      else if (args[0].startsWith("dq")) {
        // If they passed "dqw" then the "w" is for WAIT
        boolean wait = args[0].endsWith("w");
        XMLDocument dqDoc = null;
        try {
          // Dequeue the XML message
          dqDoc = xmlq.dequeue(wait);
          // Print it out
          dqDoc.print(System.out);
        }
        catch (XMLQueueEmptyException qee) {
          System.out.println("xml_msg_queue is empty.");
        }
      }
      else usage();
    }
    else usage();
  }
  private static void usage() {
    System.out.println("usage: AQ [nq ordid|dq[w]]");
    System.exit(1);
  }
}