对象创建完成后进行数据同步

 */
@Component
public class NumberPlaceDBToRedis implements ApplicationListener<ApplicationEvent> {
private static final Logger logger = Logger.getLogger(NumberPlaceDBToRedis.class);

@Autowired
private RedisTemplate redisTemplate;

@Autowired
private NumberPlaceRepository numberPlaceRepository;

private static boolean flag = true;

@Override
public void onApplicationEvent(ApplicationEvent event) {
if (flag) {
flag=false;//防止二次调用
logger.info("号码归属地数据库缓存数据同步");
try {
List<NumberPlace> numberPlaces = numberPlaceRepository.findAll();
StringBuilder stringBuilder = new StringBuilder(NumberPlaceConstant.NUMBER_PLACE);
numberPlaces.forEach(numberPlace -> {
if (numberPlace.getStatus() == 0) {
redisTemplate.opsForValue().set(stringBuilder.append(numberPlace.getProvinceId()), numberPlace.getProvinceId());
}

});
} catch (Exception e) {
logger.error("号码归属地数据库缓存数据同步失败");
throw new RuntimeException("号码归属地数据库缓存数据同步失败");
}
}
}
}
posted on 2018-12-08 20:12  知了不了了之  阅读(188)  评论(0编辑  收藏  举报