利用SpringBoot定时检查Redis链接
2017-10-31 本文已影响61人
Qihang
-
使用技术
- Spring Boot
- Scheduled
- Redis
-
服务监控入口
- 定时任务加入
@EnableScheduling
注解
- 定时任务加入
@SpringBootApplication
@EnableScheduling
public class MonitoringApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(MonitoringApplication.class, args);
}
}
- 定时任务入口
@Scheduled(cron="*/1 * * * * ?")
@Component
public class Jobs {
@Autowired
RedisService redisService;
/**
* 检查Redis链接状态
* 每隔1分钟执行一次
*/
@Scheduled(cron="*/1 * * * * ?")
public void checkRedisConnectionJob(){
try {
redisService.checkConnection();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
- 检查Redis链接处理
@Autowired
StringRedisTemplate stringRedisTemplate;
@Override
public Boolean checkConnection() throws UnsupportedEncodingException, NoSuchAlgorithmException {
RedisConnectionFactory redisConnectionFactory = stringRedisTemplate.getConnectionFactory();
RedisConnection redisConnection = redisConnectionFactory.getConnection();
Boolean flag = redisConnection.isClosed();
if (flag) {
logger.info("{} Redis Connection is Closed : {}", new Date(), flag);
CloopenUtil sendSms = new CloopenUtil();
sendSms.sendTemplateSMS("手机号", SMSConstants.TMP_CAPTCHA, new String[]{"Redis Connection Success is " + flag, "2"});
}
return !flag;
}