ITask Sample Code

package test;

import test.Task; //The generated bean
import com.attachmate.tasks.ITaskListener;
import com.attachmate.tasks.TaskStatusChangeEvent;

/**
 * A simple console application that demonstrates how to execute a task
 * via the ITask API.
 * @see com.attachmate.tasks.ITask
 * @see com.attachmate.tasks.ITaskListener
 * @see com.attachmate.tasks.TaskStatusChangeEvent
 */
public class BeanTest
{
    /**
     * Application entry point.
     * @param args Command-line arguments.  Not used.
     */
    public static void main(String[] args)
    {
        try
        {
            //Instantiate the generated bean and add a task listener
            Task task = new Task();
            TaskListener listener = new TaskListener();
            task.addTaskListener(listener);

            //Set the task inputs and execute
            task.setInput("data"); //Generated input property
            task.executeSync();

            if (task.getStatus().getStatus() == TaskStatusChangeEvent.TASK_SUCCESSFUL)
            {
                //Display the task output
                System.out.println(task.getOutput()); //Generated output property
            }
        }
        catch (Exception xx)
        {
            xx.printStackTrace();
        }
    }

    /**
     * A listener for monitoring the status of the task.
     */
    static class TaskListener implements ITaskListener
    {
        /**
         * Notification of a change in task status.
         * @param event task status change event object.
         */
        public void taskStatusChange( TaskStatusChangeEvent event )
        {
            int status = event.getStatus();
            switch (status)
            {
            case TaskStatusChangeEvent.TASK_CREATED:
            case TaskStatusChangeEvent.TASK_STARTED:
                //The task is running
                break;
            case TaskStatusChangeEvent.TASK_SUCCESSFUL:
                //The task succeeded
                break;
            case TaskStatusChangeEvent.TASK_CANCELED:
            case TaskStatusChangeEvent.TASK_DISCONNECTED:
            case TaskStatusChangeEvent.TASK_FAILED:
            case TaskStatusChangeEvent.TASK_TIMEOUT:
                //The task failed
                Exception xx = event.getException();
                if (xx != null)
                {
                    xx.printStackTrace();
                }
                break;
            }
        }
    }
}
Related Topics
Bullet Using Tasks in Your Applications, Overview
Bullet Using Java Beans
Bullet Building Applications Using Native Win32 Technologies
Bullet Proxy Service Interface API Reference
  Attachmate