ConsulHeartbeatManager { public void start() { heartbeatExecutor.scheduleAtFixedRate( new Runnable() { @Override public void run() { try { boolean switcherStatus = isHeartbeatOpen(); if (isSwitcherChange(switcherStatus)) { processHeartbeat(switcherStatus); } else { if (switcherStatus) { switcherCheckTimes++; if (switcherCheckTimes >= ConsulConstants.MAX_SWITCHER_CHECK_TIMES) { processHeartbeat(true); switcherCheckTimes = 0; } } } } catch (Exception e) { LoggerUtil.error("consul heartbeat executor err:", e); } } }, ConsulConstants.SWITCHER_CHECK_CIRCLE, ConsulConstants.SWITCHER_CHECK_CIRCLE, TimeUnit.MILLISECONDS); } ConsulHeartbeatManager(MotanConsulClient client); void start(); void close(); void addHeartbeatServcieId(String serviceid); void removeHeartbeatServiceId(String serviceid); void setHeartbeatOpen(boolean open); void setClient(MotanConsulClient client);  }
@Test public void testStart() throws InterruptedException { heartbeatManager.start(); Map<String, Long> mockServices = new HashMap<String, Long>(); int serviceNum = 5; for (int i = 0; i < serviceNum; i++) { String serviceid = "service" + i; mockServices.put(serviceid, 0L); heartbeatManager.addHeartbeatServcieId(serviceid); } setHeartbeatSwitcher(true); checkHeartbeat(mockServices, true, serviceNum); setHeartbeatSwitcher(false); Thread.sleep(100); checkHeartbeat(mockServices, false, serviceNum); }