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) {
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) {
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);
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()) {