diff --git a/client/src/main/java/com/wuba/wlock/client/communication/ServerPoolHandler.java b/client/src/main/java/com/wuba/wlock/client/communication/ServerPoolHandler.java index 864438f..3cf2bdd 100644 --- a/client/src/main/java/com/wuba/wlock/client/communication/ServerPoolHandler.java +++ b/client/src/main/java/com/wuba/wlock/client/communication/ServerPoolHandler.java @@ -31,6 +31,7 @@ import com.wuba.wlock.client.registryclient.entity.ClientKeyEntity; import com.wuba.wlock.client.registryclient.entity.NodeAddr; import com.wuba.wlock.client.registryclient.registrykey.RegistryKey; +import com.wuba.wlock.client.service.HeartbeatService; import com.wuba.wlock.client.util.TimeUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -60,6 +61,7 @@ private ServerPoolHandler(WLockClient wlockClient) throws IOException, Execution this.wlockClient = wlockClient; this.registryKey = wlockClient.getRegistryKey(); initSeverPool(); + HeartbeatService.getHbService().start(); } public void initSeverPool() throws IOException, ExecutionException, InterruptedException { diff --git a/client/src/main/java/com/wuba/wlock/client/service/HeartbeatService.java b/client/src/main/java/com/wuba/wlock/client/service/HeartbeatService.java index 9a058ee..849cc83 100644 --- a/client/src/main/java/com/wuba/wlock/client/service/HeartbeatService.java +++ b/client/src/main/java/com/wuba/wlock/client/service/HeartbeatService.java @@ -36,7 +36,7 @@ public class HeartbeatService { private static final Log logger = LogFactory.getLog(HeartbeatService.class); - public AtomicBoolean started = new AtomicBoolean(true); + public AtomicBoolean started = new AtomicBoolean(false); private int initialDelay = 30; private int period = (new Random().nextInt(4) + 2); private ConcurrentHashMap serverDetectMap = new ConcurrentHashMap(128); @@ -48,18 +48,23 @@ public class HeartbeatService { public static HeartbeatService hbService = new HeartbeatService(); private HeartbeatService() { - Thread thread = new Thread(new HeartbeatWorker()); - thread.setName("WLock HeartbeatDetectThread"); - thread.setDaemon(true); - thread.start(); - - logger.info(Version.INFO + ", start heartbeat detect job success."); } public static HeartbeatService getHbService() { return hbService; } + public void start() { + if (started.compareAndSet(false, true)) { + Thread thread = new Thread(new HeartbeatWorker()); + thread.setName("WLock HeartbeatDetectThread"); + thread.setDaemon(true); + thread.start(); + + logger.info(Version.INFO + ", start heartbeat detect job success."); + } + } + private void sendHeartbeat() { // 清理脏数据 if (clearCount.incrementAndGet() > heartbeatMaxTimes) {