Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / ExecutorFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.common.log.wrappers.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.stereotype.Component;
27
28 import java.lang.Thread.UncaughtExceptionHandler;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.ScheduledExecutorService;
32 import java.util.concurrent.ThreadFactory;
33 @Component("executorFactory")
34 /**
35  * Allows to create next kinds of single thread executors: SingleThreadExecutor and SingleThreadScheduledExecutor
36  */
37 public class ExecutorFactory {
38
39     private static final Logger logger = Logger.getLogger(EnvironmentsEngine.class.getName());
40
41     public ExecutorService create(String name, UncaughtExceptionHandler exceptionHandler){
42         logger.info("Going to create single thread executor. ");
43         ThreadFactory threadFactory = createThreadFactory(name, exceptionHandler);
44         return Executors.newSingleThreadExecutor(threadFactory);
45     }
46
47     public ScheduledExecutorService createScheduled(String name){
48         logger.info("Going to create single thread scheduled executor. ");
49         ThreadFactory threadFactory = createThreadFactory(name,
50                 (t, e) -> LoggerFactory.getLogger(UncaughtExceptionHandler.class).error("An error occurred: ", e));
51         return Executors.newSingleThreadScheduledExecutor(threadFactory);
52     }
53
54     private ThreadFactory createThreadFactory(String name, UncaughtExceptionHandler exceptionHandler) {
55         String nameFormat = name + "-%d";
56         return new ThreadFactoryBuilder()
57                 .setThreadFactory(Executors.defaultThreadFactory())
58                 .setNameFormat(nameFormat)
59                 .setUncaughtExceptionHandler(exceptionHandler)
60                 .setDaemon(true)
61                 .build();
62     }
63 }