1)整合:略。因为在Spring-context包中已经整合过了
2)使用:写一个方法,然后加上规则,他就会被定时执行
/*** 项目启动后从数据库将各设备的信息和阈值查询出来放入redis* 五分钟一次*/
//0 */5 * * * ?
@Scheduled(cron = "0 */5 * * * ?")
public void getDeviceThresholdFromRedis() {//保证redis中是最新的数据,需要定时删除redisTemplate.delete("deviceThreshold");Set deviceThreshold = redisTemplate.opsForHash().keys("deviceThreshold");if (deviceThreshold.size() < 1) {List<Device> deviceList = deviceDao.list(null, null, null);for (Device device : deviceList) {redisTemplate.opsForHash().put("deviceThreshold",device.getId(),device);}}System.out.println(deviceThreshold.size());
}
3)注意事项:因为考虑到客户的部署环境比较复杂,redis没有做高可用,可能会挂掉