WhiteIpCache.java
2.04 KB
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
package com.bsth.data;
import com.bsth.Application;
import com.bsth.XDApplication;
import com.bsth.entity.WhiteIp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class WhiteIpCache implements InitializingBean {
private final static Logger log = LoggerFactory.getLogger(WhiteIpCache.class);
@Autowired
private JdbcTemplate jdbcTemplate;
private static List<WhiteIp> whiteIps;
public static List<WhiteIp> getWhiteIps() {
return whiteIps;
}
@Override
public void afterPropertiesSet() throws Exception {
Application.mainServices.scheduleWithFixedDelay(new WhiteIpDataLoader(), 0, 1, TimeUnit.MINUTES);
}
class WhiteIpDataLoader extends Thread {
@Override
public void run() {
if (SystemParamCache.getEnabledWhiteIp()) {
loadData();
}
}
/**
* @Title: loadAllData
* @Description: TODO(加载所有数据)
*/
public int loadData() {
try {
log.info("开始加载IP白名单数据..,");
loadWhiteIp();
log.info("加载IP白名单数据成功!,");
} catch (Exception e) {
log.error("加载IP白名单数据时出现异常,", e);
}
return 0;
}
/**
* 加载IP白名单
*/
public void loadWhiteIp() {
List<WhiteIp> result = jdbcTemplate.query("select * from control_interface.bsth_c_white_ip where valid_date > now()", BeanPropertyRowMapper.newInstance(WhiteIp.class));
whiteIps = result;
}
}
}