Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / scheduledtasks / AbstractScheduleTaskRunner.java
1 package org.openecomp.sdc.be.components.scheduledtasks;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.TimeUnit;
8
9 public abstract class AbstractScheduleTaskRunner {
10     private static final Logger log = LoggerFactory.getLogger(AbstractScheduleTaskRunner.class);
11     public abstract ExecutorService getExecutorService();
12
13     protected void shutdownExecutor() {
14         ExecutorService executorService = getExecutorService();
15         if (executorService == null)
16             return;
17
18         executorService.shutdown(); // Disable new tasks from being submitted
19         try {
20             // Wait a while for existing tasks to terminate
21             if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
22                 executorService.shutdownNow(); // Cancel currently executing
23                                                 // tasks
24                 // Wait a while for tasks to respond to being cancelled
25                 if (!executorService.awaitTermination(60, TimeUnit.SECONDS))
26                     log.debug("Pool did not terminate");
27             }
28         } catch (InterruptedException ie) {
29             // (Re-)Cancel if current thread also interrupted
30             executorService.shutdownNow();
31             // Preserve interrupt status
32             Thread.currentThread().interrupt();
33         }
34     }
35 }