-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
ApolloEnvironmentManager.java
75 lines (64 loc) · 2.55 KB
/
ApolloEnvironmentManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.ctrip.apollo.client;
import com.ctrip.apollo.client.loader.ConfigLoaderFactory;
import com.ctrip.apollo.client.loader.ConfigLoaderManager;
import com.ctrip.apollo.client.model.PropertySourceReloadResult;
import com.ctrip.apollo.client.util.ConfigUtil;
import com.ctrip.apollo.core.utils.ApolloThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author Jason Song(song_s@ctrip.com)
*/
class ApolloEnvironmentManager {
private static final Logger logger = LoggerFactory.getLogger(ApolloEnvironmentManager.class);
private ConfigLoaderManager configLoaderManager;
private ConfigUtil configUtil;
private ScheduledExecutorService executorService;
private ApolloEnvironment apolloEnvironment;
private AtomicBoolean initDone;
ApolloEnvironmentManager(ApolloEnvironment apolloEnvironment) {
this.apolloEnvironment = apolloEnvironment;
this.configLoaderManager = ConfigLoaderFactory.getInstance().getConfigLoaderManager();
this.configUtil = ConfigUtil.getInstance();
this.executorService =
Executors
.newScheduledThreadPool(1,
ApolloThreadFactory.create("ApolloEnvironmentManager", true));
this.initDone = new AtomicBoolean();
}
synchronized void init() {
if (initDone.get()) {
logger.warn("ApolloEnvironmentManager init already done");
return;
}
this.apolloEnvironment.updatePropertySource(this.configLoaderManager.loadPropertySource());
this.schedulePeriodicRefresh();
initDone.set(true);
}
void schedulePeriodicRefresh() {
logger.info("Schedule periodic refresh with interval: {} {}",
configUtil.getRefreshInterval(), configUtil.getRefreshTimeUnit());
this.executorService.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
try {
updatePropertySource();
} catch (Throwable ex) {
logger.error("Refreshing config failed", ex);
}
}
}, configUtil.getRefreshInterval(), configUtil.getRefreshInterval(),
configUtil.getRefreshTimeUnit());
}
void updatePropertySource() {
PropertySourceReloadResult result = this.configLoaderManager.reloadPropertySource();
if (result.hasChanges()) {
logger.info("Found changes, refresh environment.");
this.apolloEnvironment.updatePropertySource(result.getPropertySource(), result.getChanges());
}
}
}