Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to implement a Job that can be canceled(Implementing a cancelable job that is running an indeterminate task)
How to implement a Job that can be canceled [message #1237861] Thu, 30 January 2014 15:10
Joseph Gagnon is currently offline Joseph Gagnon
Messages: 58
Registered: June 2013
Member
I have cases where I will be running a background job (using the eclipse Job API) to run some long running tasks. My plan is that once a job is launched, a pop-up will appear indicating that the job is in progress while providing the user with a cancel button to abort the job. The tasks are indeterminate in length and there is no way to monitor any progress - the job is either running (or hung) or it's done. I forgot to mention: this is an eclipse 4 RCP application, using JavaFX as the renderer.

I'm trying to figure out the right way to implement something that accomplishes this and have not had much luck finding an example I can understand and adapt to what I need. I know that for a Job, the "IStatus run(IProgressMonitor)" method is implemented to do the "work". I added a job change listener to the job in order to handle when the job is done, etc. I have also created a simple "progress dialog" that takes the job and whose purpose is to display a message, an indeterminate progress indicator and the aforementioned cancel button. The dialog attaches a job change listener so that when the job is running the dialog is shown and when it's done, the dialog is hidden. The cancel button calls the job cancel() method to request cancelation.

What I've been unable to figure out is how to implement the job logic so that it will execute the "work" to be done, while also peridocally checking the progress monitor to see if cancel has been requested and stopping the "work" if so. I'm assuming I need to spawn a separate task that will do the "work" and then go into a polling loop to check the monitor.isCanceled() periodically. If it returns true, I would then do whatever is needed to shut down the "work" task. Otherwise, the "work" task does its thing until it's done.

I'm not terribly familiar with the techniques for running concurrent tasks in Java. I'm familiar with Threads and Runnables, but only at a superficial level. I've also seen references to the ExecutorService, but don't know much about it.

Below is an exmaple of my current attempts. It doesn't compile, of course, because of the while loop.

  ...

  public void performSearch(final SearchParameters searchParams) {
    if (searchParams != null) {
      Job job = new Job("MessageSearch") {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
          System.out.println("Doing search ...");
          monitor.beginTask("MessageSearch", IProgressMonitor.UNKNOWN);

          Persister persister = new Persister();
          // Do the search (somehow).
          DBQueryTask task = new DBQueryTask(persister);

          ExecutorService executor = Executors.newSingleThreadExecutor();
          executor.execute(task);
          
          while (true) {
            if (monitor.isCanceled()) {
              executor.shutdownNow();
              return Status.CANCEL_STATUS;
            }
          }

          return Status.OK_STATUS;
        }
      };

      job.addJobChangeListener(new JobChangeAdapter() {
        @Override
        public void done(final IJobChangeEvent event) {
          uiSync.asyncExec(new Runnable() {
            @Override
            public void run() {
              System.out.println("MessageViewerController.performSearch: Job has finished: " + event.getResult());
              // TODO Get search results from persister and set into model
              // model.messageListProperty().set(TBD);
            }
          });
        }
      });

      JobProgressDialogFactory.create(job, uiSync, stage, "Retrieving messages ...");
      job.schedule();
    }
  }

  ...


Any help would be appreciated.
Previous Topic:Editor Questions - multiple editors / Form Editor in e4
Next Topic:NoSuchFieldError: LOCALIZABLE___UPDATE_LOCALIZATION on Eclipse 4 model editor
Goto Forum:
  


Current Time: Fri Aug 29 22:33:31 EDT 2014

Powered by FUDForum. Page generated in 0.07932 seconds