利用SpringBoot定时检查Redis链接

2017-10-31  本文已影响61人  Qihang
@SpringBootApplication
@EnableScheduling
public class MonitoringApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MonitoringApplication.class, args);
    }

}
@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();
        }
    }

}
@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;
    }
上一篇下一篇

猜你喜欢

热点阅读