SwingCommand part 5 – Task States and Error Handling

Task States

It is possible to find the current state of a Task instance directly by calling task.getExecutionState();
There are six ExecutionStates: NOT_RUN, PENDING, STARTED, SUCCESS, CANCELLED and ERROR.

A task starts in state NOT_RUN. During the call to command.execute(), the state will change to PENDING. Once task processing begins (which may be some time later if the task is queued for processing or is blocked waiting for a thread from a thread pool) the state moves to STARTED. The final state will be either SUCCESS , ERROR or CANCELLED. In the case that a Task finishes in ERROR, you can obtain the Exception that caused the error by calling task.getExecutionException()

If a SwingCommand task handler method ( doInEventThread() or doInBackground() ) throws an Exception the task is considered to have failed. Any TaskListeners will receive an error() callback, and the task will end in the ExecutionState.ERROR state.

Ending a SwingCommand Early

If an exception is thrown during doInBackground(), the doInEventThread() handler will not be called. Instead, processing is aborted before doInEventThread() is called. This makes sense, because in most cases we will not want to update the ui if, for example, a task loading data failed.

However, sometimes you will need to perform error handling in the Swing Event Thread, or perform some processing whether or not an error occurs or the task is cancelled. For example you may need to stop a progress animation. These cases are best handled by a TaskListener, in the error() method, or the finished() method respectively.

The finished() callback always occurs, no matter if the task succeeds, fails or is cancelled. The TaskListener methods also allow you to perform handling only on success (the success() callback).

  SwingCommand c = new SwingCommand() {
      protected Task createTask() {
          return new BackgroundTask() {

              public void doInBackground() throws Exception {
                  //this method throws an exception instead of returning
                  throw new Exception("woe");
              }

              public void doInEventThread() throws Exception {
                  //So we'll never get here!
              }
          };
      }
  };

     
  c.addTaskListener(new TaskListenerAdapter() {
     
      public void error(Task task, Throwable error) {
          JOptionPane.showMessageDialog(frame, "Failed to Load Messages");
      }
   
      public void finished(Task task) {
          //the task will finish in the ERROR state since doInBackground() threw an Exception
          assert(Task.ExecutionState.ERROR == task.getExecutionState());
       
          //we can get the error back by calling task.getExecutionException()
          Throwable cause = task.getExecutionException();
      }
  });

  c.execute();

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply