Skip to content

Commit

Permalink
Catch exception in running Job and throw onward
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillipus committed Jul 21, 2017
1 parent a396d70 commit d8ecfac
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
Expand Down Expand Up @@ -53,6 +54,16 @@
*/
public class GraficoModelExporter implements IGraficoConstants {

// Use a ProgressMonitor to cancel running Jobs and track Exception
static class ExceptionProgressMonitor extends NullProgressMonitor {
IOException ex;

void catchException(IOException ex) {
this.ex = ex;
setCanceled(true);
}
}

/**
* ResourceSet
*/
Expand Down Expand Up @@ -117,6 +128,8 @@ public void exportModel() throws IOException {
// Now save all Resources
JobGroup jobgroup = new JobGroup("GraficoModelExporter", 0, 1); //$NON-NLS-1$

final ExceptionProgressMonitor pm = new ExceptionProgressMonitor();

for(Resource resource : fResourceSet.getResources()) {
Job job = new Job("Resource Save Job") { //$NON-NLS-1$
@Override
Expand All @@ -125,11 +138,12 @@ protected IStatus run(IProgressMonitor monitor) {
resource.save(null);
}
catch(IOException ex) {
ex.printStackTrace();
pm.catchException(ex);
}
return Status.OK_STATUS;
}
};

job.setJobGroup(jobgroup);
job.schedule();
}
Expand All @@ -138,13 +152,17 @@ protected IStatus run(IProgressMonitor monitor) {
@Override
public void run() {
try {
jobgroup.join(0, null);
jobgroup.join(0, pm);
}
catch(OperationCanceledException | InterruptedException ex) {
ex.printStackTrace();
}
}
});

// Throw on any exception
if(pm.ex != null) {
throw pm.ex;
}
}

/**
Expand Down

0 comments on commit d8ecfac

Please sign in to comment.