2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.distribution.engine;
23 import com.google.common.util.concurrent.ThreadFactoryBuilder;
24 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
27 import java.util.concurrent.*;
29 public class NotificationExecutorService {
31 private static final Logger logger = Logger.getLogger(NotificationExecutorService.class.getName());
33 public ExecutorService createExcecutorService(DistributionNotificationTopicConfig distributionNotificationTopic) {
35 Integer minThreadPoolSize = distributionNotificationTopic.getMinThreadPoolSize();
36 if (minThreadPoolSize == null) {
37 minThreadPoolSize = 0;
40 Integer maxThreadPoolSize = distributionNotificationTopic.getMaxThreadPoolSize();
41 if (maxThreadPoolSize == null) {
42 maxThreadPoolSize = 10;
45 ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
46 threadFactoryBuilder.setNameFormat("distribution-notification-thread-%d");
47 ThreadFactory threadFactory = threadFactoryBuilder.build();
49 return new ThreadPoolExecutor(minThreadPoolSize, maxThreadPoolSize, 60L, TimeUnit.SECONDS, new SynchronousQueue<>(), threadFactory);
52 public void shutdownAndAwaitTermination(ExecutorService pool, long maxTimeToWait) {
54 logger.debug("shutdown NotificationExecutorService");
55 pool.shutdown(); // Disable new tasks from being submitted
57 // Wait a while for existing tasks to terminate
58 if (!pool.awaitTermination(maxTimeToWait, TimeUnit.SECONDS)) {
59 pool.shutdownNow(); // Cancel currently executing tasks
60 // Wait a while for tasks to respond to being cancelled
61 if (!pool.awaitTermination(maxTimeToWait, TimeUnit.SECONDS)) {
62 logger.debug("Failed to close executor service");
65 } catch (InterruptedException ie) {
66 // (Re-)Cancel if current thread also interrupted
68 // Preserve interrupt status
69 Thread.currentThread().interrupt();