您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页利用SpringBoot定时检查Redis链接

利用SpringBoot定时检查Redis链接

来源:二三四教育网
  • 使用技术

    • 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;
    }

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务