WorkPerformerSample

This sample demonstrates how to retrieve, modify, and complete a step using the VWWorkObject class. Run the sample by entering a command similar to the following:

java WorkPerformerSample username password <server name>:<port number>/<router instance name> queueName [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.

Methods

The WorkPerformerSample class contains five methods: main(String args[]), run( ), processQueue(VWQueue vwQueue), processWork(VWWorkObject workObject), and constructor WorkPerformerSample(VWSession vwSession, Logger logger, String queueName).

main(String args[])

The main method uses common techniques for validating and defaulting argument values. The default value for the log output file is WorkPerformerSample.out. 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, the user name, and the queue name to the constructor method.

WorkPerformerSample(VWSession vwSession, Logger logger, String queueName)

The constructor WorkPerformerSample(VWSession, Logger, String) performs common exception handling and retrieves a VWQueue object of class scope with the sample method queueHelper.getQueue(). A class-scope boolean variable (m_bDone) is initialized outside the constructor. This variable serves as a control flag for terminating the run program.

A character variable to hold input keystrokes from the user is initialized within the constructor. A thread is started, and a message tells the user to hit the enter key to exit. The code captures user keystroke input in a loop and tests whether a newline character was entered.

// Start the process thread.

Thread thread = new Thread(this, "WorkPerformerSample");
thread.start();

// Wait for a keystroke.

System.out.print("Hit Enter key to exit:");
while (!m_bDone){

try {

nCh = System.in.read();
if (nCh < 0 || (char)nCh == '\n')

m_bDone = true;

}catch(java.io.IOException e)
{

m_bDone = true;

}

}

Note that control exits this loop when a new line is entered. Control does not pass to the termination code until the thread that was started terminates, as shown below:

System.out.print("Finishing processing - please wait.");
while (thread.isAlive());

The termination code in main() reports the completion of the program to the user.

finally {
if (m_logger != null)

m_logger.logAndDisplay("~ WorkPerformerSample execution complete.\n");

}

The additional constructor code that follows the above loop performs routine messaging and error handling.

After the program invokes the Java method thread.start(), the code in WorkPerformerSample.run() activates. This is the run process.

void run()

This method executes a simple loop that tests the boolean "done" flag (m_bDone) and searches the queue every 30 seconds. Other included code performs routine error handling. Note that if the mbDone flag is true, this method and the thread it runs in terminate. Otherwise, the m_bDone flag does not allow the parent object to terminate.

while (!m_bDone)
{

// Search the queue.

processQueue(m_vwQueue);

// Pause 30 seconds.

if (!m_bDone)

Thread.sleep(30000);

}

private void processQueue(VWQueue vwQueue)

Initialize null VWQueueQuery and VWWorkObject objects:

VWQueueQuery qQuery = null;
VWWorkObject workObject = null;

Set a buffer size for querying the queue that was passed in and construct a query object.

vwQueue.setBufferSize(25);
qQuery = vwQueue.createQuery(null, null, null, 0, null, null, VWFetchType.FETCH_TYPE_WORKOBJECT);

After testing for a valid query object, query for all step elements. For each element found, process it with the processWork() method.

while (qQuery.hasNext()) {

// Get each work object and process it.

workObject = (VWWorkObject)qQuery.next();
if (workObject != null)

processWork(workObject);

}

private void processWork(VWWorkObject workObject)

Each work object obtained from the query of the user-selected queue is processed in this method by work object methods VWWorkObject.getFieldValue(), VWWorkObject.setFieldValue(), and VWWorkObject.doDispatch().

Before processing a workObject, it must be locked with the VWWorkObject.doLock(boolean) method.

workObject.doLock(true);

The program tests each work object for the correct name of any fields to be retrieved or set, using VWWorkObject.hasFieldName(String). Two examples are as follows:

if (workObject.hasFieldName("Title")) {

title = (String)workObject.getFieldValue("Title");

}

// Set the comment.

if (workObject.hasFieldName("F_Comment")) {

workObject.setFieldValue("F_Comment", "Processed by WorkPerformer", true);

}

After retrieving and setting field values in the work object, the work object is dispatched with the doDispatch() method. A work object should be locked when it is dispatched.

workObject.doDispatch();