美文网首页
利用SpringBoot定时检查Redis链接

利用SpringBoot定时检查Redis链接

作者: Qihang | 来源:发表于2017-10-31 18:40 被阅读61次
    • 使用技术

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

    相关文章

      网友评论

          本文标题:利用SpringBoot定时检查Redis链接

          本文链接:https://www.haomeiwen.com/subject/dqxipxtx.html