19 package org.sleuthkit.autopsy.threadutils;
21 import java.util.List;
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.ScheduledThreadPoolExecutor;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import java.util.concurrent.atomic.AtomicLong;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
40 private static final AtomicLong
totalTasks =
new AtomicLong();
63 this.timeUnit = TimeUnit.SECONDS;
76 if (delay == null || delay < 0) {
77 throw new IllegalArgumentException(String.format(
"Argument for delay parameter = %d, must be zero or any positive integer", delay));
79 if (timeUnit == null) {
80 throw new IllegalArgumentException(
"Argument for timeUnit parameter is null");
98 public TaskAttempt(Long delay, Long timeOut, TimeUnit timeUnit) {
99 if (delay == null || delay < 0) {
100 throw new IllegalArgumentException(String.format(
"Argument for delay parameter = %d, must be zero or any positive integer", delay));
102 if (timeOut == null || timeOut < 0) {
103 throw new IllegalArgumentException(String.format(
"Argument for timeOut parameter = %d, must be zero or any positive integer", delay));
105 if (timeUnit == null) {
106 throw new IllegalArgumentException(
"Argument for timeUnit parameter is null");
183 public static <T> T
attemptTask(Callable<T> task, List<TaskAttempt> attempts, ScheduledThreadPoolExecutor executor,
Terminator terminator, Logger logger, String taskDesc)
throws InterruptedException {
185 String taskDescForLog = taskDesc != null ? taskDesc :
"Task";
186 int attemptCounter = 0;
187 if (attempts.size() > 0) {
188 totalTasks.incrementAndGet();
190 while (result == null && attemptCounter < attempts.size()) {
191 if (terminator != null && terminator.stopTaskAttempts()) {
192 if (logger != null) {
193 logger.log(Level.WARNING, String.format(
"Attempts to execute '%s' terminated ", taskDescForLog));
197 TaskAttempt attempt = attempts.get(attemptCounter);
198 if (logger != null) {
199 logger.log(Level.INFO, String.format(
"SCHEDULING '%s' (attempt = %d, delay = %d %s, timeout = %d %s)", taskDescForLog, attemptCounter + 1, attempt.
getDelay(), attempt.
getTimeUnit(), attempt.
getTimeout(), attempt.
getTimeUnit()));
201 if (attemptCounter > 0) {
202 totalTaskRetries.incrementAndGet();
204 ScheduledFuture<T> future = executor.schedule(task, attempt.
getDelay(), attempt.
getTimeUnit());
207 }
catch (InterruptedException ex) {
208 if (logger != null) {
209 logger.log(Level.SEVERE, String.format(
"Interrupted executing '%s'", taskDescForLog), ex);
212 }
catch (ExecutionException ex) {
213 if (logger != null) {
214 logger.log(Level.SEVERE, String.format(
"Error executing '%s'", taskDescForLog), ex);
216 }
catch (TimeoutException ex) {
217 if (logger != null) {
218 logger.log(Level.SEVERE, String.format(
"Time out executing '%s'", taskDescForLog), ex);
220 totalTaskAttemptTimeOuts.incrementAndGet();
225 if (result == null) {
226 if (terminator == null || !terminator.stopTaskAttempts()) {
227 totalFailedTasks.incrementAndGet();
239 return totalTasks.get();
248 return totalTaskRetries.get();
257 return totalTaskAttemptTimeOuts.get();
267 return totalFailedTasks.get();
boolean stopTaskAttempts()
static final AtomicLong totalTaskAttemptTimeOuts
static< T > T attemptTask(Callable< T > task, List< TaskAttempt > attempts, ScheduledThreadPoolExecutor executor, Terminator terminator, Logger logger, String taskDesc)
static long getTotalTasksCount()
static long getTotalTaskAttemptTimeOutsCount()
static final AtomicLong totalFailedTasks
TaskAttempt(Long delay, TimeUnit timeUnit)
static long getTotalTaskRetriesCount()
static final AtomicLong totalTaskRetries
static long getTotalFailedTasksCount()
static final AtomicLong totalTasks
TaskAttempt(Long delay, Long timeOut, TimeUnit timeUnit)