Fix beat task never execute bug

This commit is contained in:
nkorange 2018-08-03 09:09:52 +08:00
parent cc33ccd1ad
commit a8ead77f6f

View File

@ -23,21 +23,21 @@ import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author harold
*/
public class BeatReactor {
private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName("com.alibaba.nacos.naming.beat.sender");
return thread;
}
private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(r -> {
Thread thread = new Thread(r);
thread.setDaemon(true);
thread.setName("com.alibaba.nacos.naming.beat.sender");
return thread;
});
private long clientBeatInterval = 10 * 1000;
@ -48,7 +48,7 @@ public class BeatReactor {
public BeatReactor(NamingProxy serverProxy) {
this.serverProxy = serverProxy;
executorService.execute(new BeatProcessor());
executorService.scheduleAtFixedRate(new BeatProcessor(), 0, clientBeatInterval, TimeUnit.MILLISECONDS);
}
public void addBeatInfo(String dom, BeatInfo beatInfo) {
@ -63,18 +63,14 @@ public class BeatReactor {
@Override
public void run() {
while (true) {
try {
for (Map.Entry<String, BeatInfo> entry : dom2Beat.entrySet()) {
BeatInfo beatInfo = entry.getValue();
executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS);
LogUtils.LOG.info("BEAT", "send beat to server: ", beatInfo.toString());
}
TimeUnit.MILLISECONDS.sleep(clientBeatInterval);
} catch (Exception e) {
LogUtils.LOG.error("CLIENT-BEAT", "Exception while scheduling beat.", e);
try {
for (Map.Entry<String, BeatInfo> entry : dom2Beat.entrySet()) {
BeatInfo beatInfo = entry.getValue();
executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS);
LogUtils.LOG.info("BEAT", "send beat to server: ", beatInfo.toString());
}
} catch (Exception e) {
LogUtils.LOG.error("CLIENT-BEAT", "Exception while scheduling beat.", e);
}
}
}