b890014034585055bc6f87f2185c674f89d948b9
[appc.git] / appc-dispatcher / appc-dispatcher-common / execution-queue-management-lib / src / main / java / org / onap / appc / executionqueue / helper / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.executionqueue.helper;
27
28 import org.onap.appc.configuration.Configuration;
29 import org.onap.appc.configuration.ConfigurationFactory;
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.ThreadFactory;
34 import java.util.concurrent.atomic.AtomicInteger;
35
36 public class Util {
37
38     private int default_queue_size = 10;
39     private int default_threadpool_size = 10;
40     private String queue_size_key = "appc.dispatcher.executionqueue.backlog.size";
41     private String threadpool_size_key = "appc.dispatcher.executionqueue.threadpool.size";
42     private EELFLogger logger = EELFManager.getInstance().getLogger(Util.class);
43
44     private Configuration configuration;
45
46     /**
47      * Initialization.
48      * <p>Used by blueprint.
49      */
50     public void init() {
51         configuration = ConfigurationFactory.getConfiguration();
52     }
53
54     public int getExecutionQueueSize() {
55         String sizeStr = configuration.getProperty(queue_size_key, String.valueOf(default_queue_size));
56
57         int size = default_queue_size;
58         try {
59             size = Integer.parseInt(sizeStr);
60         } catch (NumberFormatException e) {
61             logger.error("Error parsing dispatcher execution queue backlog size", e);
62         }
63
64         return size;
65     }
66
67     public int getThreadPoolSize() {
68         String sizeStr = configuration.getProperty(threadpool_size_key, String.valueOf(default_threadpool_size));
69
70         int size = default_threadpool_size;
71         try {
72             size = Integer.parseInt(sizeStr);
73         } catch (NumberFormatException e) {
74             logger.error("Error parsing dispatcher execution queue threadpool size", e);
75         }
76
77         return size;
78     }
79
80     public ThreadFactory getThreadFactory(final boolean isDaemon, final String threadNamePrefix) {
81         return new ThreadFactory() {
82             private final String THREAD_NAME_PATTERN = "%s-%d";
83             private final ThreadFactory factory = Executors.defaultThreadFactory();
84             private final AtomicInteger counter = new AtomicInteger();
85
86             public Thread newThread(Runnable r) {
87                 Thread t = factory.newThread(r);
88                 t.setDaemon(isDaemon);
89                 if (threadNamePrefix != null && !threadNamePrefix.isEmpty()) {
90                     final String threadName = String.format(THREAD_NAME_PATTERN, threadNamePrefix, counter.incrementAndGet());
91                     t.setName(threadName);
92                 }
93                 return t;
94             }
95         };
96     }
97 }