Skip to content

Commit

Permalink
[ISSUE-#4631] Remove timer,Use ScheduledThreadPoolExecutor replaced. (#…
Browse files Browse the repository at this point in the history
…4635)

* fix(CredentialWatcher): 使用ScheduledThreadPoolExecutor替代Timer

* enhance(CredentialWatcher): use ExecutorFactory replace ScheduledThreadPoolExecutor.

* enhance(CredentialWatcher): use ExecutorFactory.Managed replace ExecutorFactory.

* enhance(CredentialWatcher): remove Managed
  • Loading branch information
onewe authored Jan 8, 2021
1 parent 24d0cba commit 79fddc3
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.alibaba.nacos.client.identify;

import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.common.utils.StringUtils;
import org.slf4j.Logger;

Expand All @@ -27,8 +29,8 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Credential Watcher.
Expand All @@ -39,32 +41,30 @@ public class CredentialWatcher {

private static final Logger SPAS_LOGGER = LogUtils.logger(CredentialWatcher.class);

private static final long REFRESH_INTERVAL = 10 * 1000;
private static final long REFRESH_INTERVAL = 10 * 1000L;

private final CredentialService serviceInstance;

private final String appName;

private String propertyPath;

private final TimerTask watcher;

private boolean stopped;

private final ScheduledExecutorService executor;

@SuppressWarnings("PMD.AvoidUseTimerRule")
public CredentialWatcher(String appName, CredentialService serviceInstance) {
this.appName = appName;
this.serviceInstance = serviceInstance;
loadCredential(true);
watcher = new TimerTask() {
private final Timer timer = new Timer(true);


executor = ExecutorFactory
.newSingleScheduledExecutorService(new NameThreadFactory("com.alibaba.nacos.client.identify.watcher"));

executor.scheduleWithFixedDelay(new Runnable() {
private long modified = 0;

{
timer.schedule(this, REFRESH_INTERVAL, REFRESH_INTERVAL);
}

@Override
public void run() {
synchronized (this) {
Expand All @@ -87,7 +87,7 @@ public void run() {
}
}
}
};
}, REFRESH_INTERVAL, REFRESH_INTERVAL, TimeUnit.MILLISECONDS);
}

/**
Expand All @@ -97,10 +97,10 @@ public void stop() {
if (stopped) {
return;
}
if (watcher != null) {
synchronized (watcher) {
watcher.cancel();
if (executor != null) {
synchronized (executor) {
stopped = true;
executor.shutdown();
}
}
SPAS_LOGGER.info("[{}] {} is stopped", appName, this.getClass().getSimpleName());
Expand Down

0 comments on commit 79fddc3

Please sign in to comment.