-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
ApolloRefreshConfig.java
37 lines (31 loc) · 1.27 KB
/
ApolloRefreshConfig.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
package com.ctrip.framework.apollo.demo.spring.common.refresh;
import com.ctrip.framework.apollo.demo.spring.common.bean.AnnotatedBean;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;
/**
* To refresh the config bean when config is changed
*
* @author Jason Song(song_s@ctrip.com)
*/
@Component
public class ApolloRefreshConfig {
private static final Logger logger = LoggerFactory.getLogger(ApolloRefreshConfig.class);
@Autowired
private RefreshScope refreshScope;
@Autowired
private AnnotatedBean annotatedBean;
@ApolloConfigChangeListener({"application", "FX.apollo"})
private void onChange(ConfigChangeEvent changeEvent) {
if (changeEvent.isChanged("timeout") || changeEvent.isChanged("batch")) {
logger.info("before refresh {}", annotatedBean.toString());
//could also call refreshScope.refreshAll();
refreshScope.refresh("annotatedBean");
logger.info("after refresh {}", annotatedBean.toString());
}
}
}