Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-#4631] Remove timer,Use ScheduledThreadPoolExecutor replaced. #4635

Merged
merged 4 commits into from
Jan 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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