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