4955cf41f82ae258f9d5ac84086ca9bccd8df1f8
[sdc.git] /
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 java.util.concurrent.ExecutorService;
24 import java.util.concurrent.SynchronousQueue;
25 import java.util.concurrent.ThreadFactory;
26 import java.util.concurrent.ThreadPoolExecutor;
27 import java.util.concurrent.TimeUnit;
28
29 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.util.concurrent.ThreadFactoryBuilder;
34
35 public class NotificationExecutorService {
36
37     private static final Logger logger = LoggerFactory.getLogger(NotificationExecutorService.class);
38
39     public ExecutorService createExcecutorService(DistributionNotificationTopicConfig distributionNotificationTopic) {
40
41         Integer minThreadPoolSize = distributionNotificationTopic.getMinThreadPoolSize();
42         if (minThreadPoolSize == null) {
43             minThreadPoolSize = 0;
44         }
45
46         Integer maxThreadPoolSize = distributionNotificationTopic.getMaxThreadPoolSize();
47         if (maxThreadPoolSize == null) {
48             maxThreadPoolSize = 10;
49         }
50
51         ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
52         threadFactoryBuilder.setNameFormat("distribution-notification-thread-%d");
53         ThreadFactory threadFactory = threadFactoryBuilder.build();
54
55         ExecutorService executorService = new ThreadPoolExecutor(minThreadPoolSize, maxThreadPoolSize, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);
56
57         return executorService;
58     }
59
60     public void shutdownAndAwaitTermination(ExecutorService pool, long maxTimeToWait) {
61
62         logger.debug("shutdown NotificationExecutorService");
63         pool.shutdown(); // Disable new tasks from being submitted
64         try {
65             // Wait a while for existing tasks to terminate
66             if (!pool.awaitTermination(maxTimeToWait, TimeUnit.SECONDS)) {
67                 pool.shutdownNow(); // Cancel currently executing tasks
68                 // Wait a while for tasks to respond to being cancelled
69                 if (!pool.awaitTermination(maxTimeToWait, TimeUnit.SECONDS)) {
70                     logger.debug("Failed to close executor service");
71                 }
72             }
73         } catch (InterruptedException ie) {
74             // (Re-)Cancel if current thread also interrupted
75             pool.shutdownNow();
76             // Preserve interrupt status
77             Thread.currentThread().interrupt();
78         }
79     }
80
81 }