1 package org.onap.msb.apiroute.health;
3 import java.text.SimpleDateFormat;
5 import org.onap.msb.apiroute.wrapper.util.JedisUtil;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
9 import redis.clients.jedis.Jedis;
11 import com.codahale.metrics.health.HealthCheck;
13 public class RedisHealthCheck extends HealthCheck implements Runnable {
14 private static final Logger LOGGER = LoggerFactory
15 .getLogger(RedisHealthCheck.class);
17 public static boolean writeCheckFlag = true;
18 private static Result result = Result.healthy();
20 private static int failedLoopCheckNum = 12;
21 private static int failedTimer = 5 * 1000;
23 private static int normalTimer = 20 * 1000;
25 public static Result getResult() {
30 protected Result check() {
34 Result writeCheckResult = checkWrite();
35 if (writeCheckResult.isHealthy()) {
36 writeCheckFlag = false;
40 if (!writeCheckResult.isHealthy()) {
41 return writeCheckResult;
46 Result readCheckResult = checkRead();
49 if (!readCheckResult.isHealthy()) {
50 return readCheckResult;
53 return Result.healthy();
56 private Result checkRead() {
57 Jedis jedisHandle = null;
59 Result healthRst = Result.healthy();
62 jedisHandle = JedisUtil.borrowJedisInstance();
63 jedisHandle.get("healthchek:checktime");
65 } catch (Exception e) {
66 LOGGER.warn("RedisHealthCheck exception", e);
67 healthRst = Result.unhealthy(e);
69 JedisUtil.returnJedisInstance(jedisHandle);
75 private Result checkWrite() {
76 Jedis jedisHandle = null;
78 Result healthRst = Result.healthy();
81 long currentTime = System.currentTimeMillis();
82 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
83 String date = sdf.format(currentTime);
85 jedisHandle = JedisUtil.borrowJedisInstance();
86 String statusCode = jedisHandle.set("healthchek:checktime", date);
88 if (statusCode != null && statusCode.equals("OK")) {
89 healthRst = Result.healthy("check redis:" + statusCode);
91 healthRst = Result.unhealthy("check redis:" + statusCode);
94 } catch (Exception e) {
95 LOGGER.warn("RedisHealthCheck exception", e);
96 healthRst = Result.unhealthy(e);
98 JedisUtil.returnJedisInstance(jedisHandle);
106 // TODO Auto-generated method stub
109 if (LOGGER.isDebugEnabled()) {
110 LOGGER.debug("redis check starttime:"
111 + System.currentTimeMillis());
114 result = checkWithPolicy();
116 if (LOGGER.isDebugEnabled()) {
117 LOGGER.debug("redis check result:" + result.isHealthy()
118 + " message:" + result.getMessage());
120 LOGGER.debug("redis check endtime:"
121 + System.currentTimeMillis());
125 Thread.sleep(normalTimer);
126 } catch (InterruptedException e) {
127 // TODO Auto-generated catch block
128 LOGGER.warn("loop check redis,thread sleep excepiton", e);
133 private Result checkWithPolicy() {
135 Result temp = Result.healthy();
142 if (temp.isHealthy()) {
150 Thread.sleep(failedTimer);
151 } catch (InterruptedException e) {
152 // TODO Auto-generated catch block
153 LOGGER.warn("loop check redis,thread sleep excepiton", e);
156 } while (failedNum <= failedLoopCheckNum);