Custom Task Guide

Custom Task Guide

Pausing and Canceling Tasks

Pausing and Canceling Tasks

Managed File Transfer
Jobs, or actively running Projects, can be canceled or paused and later resumed. In order to support cancel, pause and resume, the task author must periodically check for any pending cancel or pause requests. As a task author, you need to call the checkForHoldCancel() method periodically from a long running section of code. In our example, we will check for cancel/pause requests in the while loop of the copy method as shown below:
Please note that the red text denotes the new source added to the example.
private long copy() throws IOException { InputStream in = null; OutputStream out = null; try { final int bufferSize = 4096; in = new BufferedInputStream(new FileInputStream(sourceFile), bufferSize); out = new BufferedOutputStream(new FileOutputStream(new File( destinationDirectory, sourceFile.getName())), bufferSize); byte[] buffer = new byte[bufferSize]; int bytesRead = 0; long bytesCopied = 0L; while ((bytesRead = in.read(buffer)) != -1) { checkForHoldAndCancel(); out.write(buffer, 0, bytesRead); bytesCopied += bytesRead; } return bytesCopied; } finally { if (in != null) { try { in.close(); } catch (IOException exp) { exp.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException exp) { exp.printStackTrace(); } } } }
The call to the checkForHoldAndCancel will do the work by either cancelling the Job or pausing the Job (thread) if the user requested so. Cancelation of a Job is achieved by throwing an exception, ExecutionCancelledException, which is a RuntimeException. The Project Runtime catches this exception and marks the job as canceled in the audit logs. Pausing is achieved by calling the Thread.wait() on the thread that is executing the Job. The thread will wait until someone requests to resume the Job.

0 COMMENTS

We’d like to hear from you!