This sample demonstrates how to access and display queue contents, using the sample QueueHelper(VWSession, Logger) class to display queue information. Run the sample by entering a command similar to the following:
java QueueSample username password <server name>:<port
number>/<router instance name> [queue_name] [output_filename]
Note For a detailed explanation of the command line, see the Run the sample application section of the Run the Unmodified Samples topic.
The QueueSample class contains two methods: the main(String args[]) method and the QueueSample(VWSession vwSession, Logger logger, String queueName) method, which is the constructor.
The main method uses common techniques for validating and defaulting argument values. The default values for the log output file and queue names are QueueSample.out and the first queue name in the system list of queue names, respectively. The main method constructs and passes vwSession and Logger objects to the sample constructor. Main() handles the login and logoff for the session with the login() and logoff() methods of the sample SessionHelper class. It provides workflow logging with an instance of the sample Logger class. The main method passes the session, the logger, and the queue name, which may be NULL.
After the constructor QueueSample(VWSession, Logger, queueName) performs common exception handling, the method invokes methods from the sample queueHelper class and the APi class VWQueue to display queue contents. The code is organized as follows:
Create the queue helper object.
queueHelper = new QueueHelper(vwSession, logger);
Get the queue object for the queueName passed in, or get first queue in the system list of queueNames.
if (queueName != null) { vwQueue = vwSession.getQueue(queueName);
} else {
String[] queueNames = queueHelper.getQueueNames(false);
if (queueNames == null || queueNames.length == 0){
logger.log("No queues found.");
return;
} else {
Iterate through the array getting queues until first one with available elements is found.
for (int i = 0; i < queueNames.length; i++){
vwQueue = vwSession.getQueue(queueNames[i]);
if (vwQueue != null) {
if (vwQueue.fetchCount() > 0)break;
}
// Clear our reference.
vwQueue = null;
}
}
}
// Ensure there is a VWQueue object.
if (vwQueue == null){
logger.log("Unable to retrieve a queue!");
return;
} else {
Display the contents of the queue:
queueHelper.displayQueueContents(vwQueue);
}
Additional code in this sample manages common cleanup and error handling.