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 {
 
  188         String taskDescForLog = taskDesc != null ? taskDesc : 
"Task";
 
  189         int attemptCounter = 0;
 
  190         if (attempts.size() > 0) {
 
  191             totalTasks.incrementAndGet();
 
  193         while (result == null && attemptCounter < attempts.size()) {
 
  194             if (terminator != null && terminator.stopTaskAttempts()) {
 
  195                 if (logger != null) {
 
  196                     logger.log(Level.WARNING, String.format(
"Attempts to execute '%s' terminated ", taskDescForLog));
 
  200             TaskAttempt attempt = attempts.get(attemptCounter);
 
  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();
 
  229         if (logger != null && attemptCounter > 1) {
 
  230             if (result != null) {
 
  231                 logger.log(Level.WARNING, String.format(
"'%s' succeeded after %d attempts", taskDescForLog, attemptCounter));
 
  233                 logger.log(Level.SEVERE, String.format(
"'%s' failed after %d attempts", taskDescForLog, attemptCounter));
 
  240         if (result == null) {
 
  241             if (terminator == null || !terminator.stopTaskAttempts()) {
 
  242                 totalFailedTasks.incrementAndGet();
 
  255         return totalTasks.get();
 
  264         return totalTaskRetries.get();
 
  273         return totalTaskAttemptTimeOuts.get();
 
  283         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)