Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / NotificationExecutorService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine;
22
23 import com.google.common.util.concurrent.ThreadFactoryBuilder;
24 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.concurrent.*;
29
30 public class NotificationExecutorService {
31
32     private static final Logger logger = LoggerFactory.getLogger(NotificationExecutorService.class);
33
34     public ExecutorService createExcecutorService(DistributionNotificationTopicConfig distributionNotificationTopic) {
35
36         Integer minThreadPoolSize = distributionNotificationTopic.getMinThreadPoolSize();
37         if (minThreadPoolSize == null) {
38             minThreadPoolSize = 0;
39         }
40
41         Integer maxThreadPoolSize = distributionNotificationTopic.getMaxThreadPoolSize();
42         if (maxThreadPoolSize == null) {
43             maxThreadPoolSize = 10;
44         }
45
46         ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
47         threadFactoryBuilder.setNameFormat("distribution-notification-thread-%d");
48         ThreadFactory threadFactory = threadFactoryBuilder.build();
49
50         ExecutorService executorService = new ThreadPoolExecutor(minThreadPoolSize, maxThreadPoolSize, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);
51
52         return executorService;
53     }
54
55     public void shutdownAndAwaitTermination(ExecutorService pool, long maxTimeToWait) {
56
57         logger.debug("shutdown NotificationExecutorService");
58         pool.shutdown(); // Disable new tasks from being submitted
59         try {
60             // Wait a while for existing tasks to terminate
61             if (!pool.awaitTermination(maxTimeToWait, TimeUnit.SECONDS)) {
62                 pool.shutdownNow(); // Cancel currently executing tasks
63                 // Wait a while for tasks to respond to being cancelled
64                 if (!pool.awaitTermination(maxTimeToWait, TimeUnit.SECONDS)) {
65                     logger.debug("Failed to close executor service");
66                 }
67             }
68         } catch (InterruptedException ie) {
69             // (Re-)Cancel if current thread also interrupted
70             pool.shutdownNow();
71             // Preserve interrupt status
72             Thread.currentThread().interrupt();
73         }
74     }
75
76 }