Skip to content

Commit

Permalink
Fix beat task never execute bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Aug 3, 2018
1 parent cc33ccd commit a8ead77
Showing 1 changed file with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@

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;
Expand All @@ -48,7 +48,7 @@ public Thread newThread(Runnable r) {

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) {
Expand All @@ -63,18 +63,14 @@ class BeatProcessor implements Runnable {

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

0 comments on commit a8ead77

Please sign in to comment.