• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.datasources.utils.quartzmanager;
17 
18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.ScheduledThreadPoolExecutor;
27 import java.util.concurrent.TimeUnit;
28 
29 /**
30  * timed task management
31  *
32  * @since 2021/5/19 16:39
33  */
34 public class QuartzManager {
35     /**
36      * DELAY 0
37      */
38     public static final int DELAY = 0;
39 
40     /**
41      * PERIOD 4000
42      */
43     public static final int PERIOD = 4000;
44     private static final Logger LOGGER = LogManager.getLogger(QuartzManager.class);
45     private static volatile QuartzManager instance;
46 
47     private Map<String, Runnable> runnableHashMap = new ConcurrentHashMap<>();
48     private Map<String, ScheduledExecutorService> executorHashMap = new ConcurrentHashMap<>();
49 
50     /**
51      * getInstance
52      *
53      * @return QuartzManager
54      */
getInstance()55     public static QuartzManager getInstance() {
56         if (instance == null) {
57             synchronized (QuartzManager.class) {
58                 if (instance == null) {
59                     instance = new QuartzManager();
60                 }
61             }
62         }
63         return instance;
64     }
65 
66     /**
67      * execution
68      *
69      * @param runName runName
70      * @param runnable runnable
71      */
addExecutor(String runName, Runnable runnable)72     public void addExecutor(String runName, Runnable runnable) {
73         if (ProfilerLogManager.isInfoEnabled()) {
74             LOGGER.info("addExecutor {}", runName);
75         }
76         ScheduledExecutorService scheduled = new ScheduledThreadPoolExecutor(1);
77         executorHashMap.put(runName, scheduled);
78         runnableHashMap.put(runName, runnable);
79     }
80 
81     /**
82      * begin Execution
83      *
84      * @param runName runName
85      * @param delay delay
86      * @param period period
87      */
startExecutor(String runName, long delay, long period)88     public void startExecutor(String runName, long delay, long period) {
89         if (ProfilerLogManager.isInfoEnabled()) {
90             LOGGER.info("startExecutor {}", runName);
91         }
92         ScheduledExecutorService scheduled = executorHashMap.get(runName);
93         Runnable runnable = runnableHashMap.get(runName);
94         if (delay > 0) {
95             scheduled.scheduleWithFixedDelay(runnable, delay, period, TimeUnit.MILLISECONDS);
96         } else {
97             scheduled.scheduleAtFixedRate(runnable, 0, period, TimeUnit.MILLISECONDS);
98         }
99     }
100 
101     /**
102      * deleteExecutor
103      *
104      * @param runName runName
105      */
deleteExecutor(String runName)106     public void deleteExecutor(String runName) {
107         if (ProfilerLogManager.isInfoEnabled()) {
108             LOGGER.info("deleteExecutor {}", runName);
109         }
110         ScheduledExecutorService scheduledExecutorService = executorHashMap.get(runName);
111         if (scheduledExecutorService != null) {
112             scheduledExecutorService.shutdown();
113             if (executorHashMap != null && executorHashMap.size() != 0) {
114                 executorHashMap.remove(runName);
115             }
116             if (runnableHashMap != null && runnableHashMap.size() != 0) {
117                 runnableHashMap.remove(runName);
118             }
119         }
120     }
121 
122     /**
123      * checkService
124      *
125      * @param runName runName
126      * @return ScheduledExecutorService
127      */
checkService(String runName)128     public Optional<ScheduledExecutorService> checkService(String runName) {
129         if (ProfilerLogManager.isInfoEnabled()) {
130             LOGGER.info("checkService");
131         }
132         ScheduledExecutorService scheduledExecutorService = executorHashMap.get(runName);
133         return Optional.ofNullable(scheduledExecutorService);
134     }
135 }
136