Skip to content

Commit

Permalink
Merge pull request #409 from confluentinc/am-cert
Browse files Browse the repository at this point in the history
KREST-11276 - Fixing cert reload with multiple registered listeners
  • Loading branch information
apoorvmittal10 committed Aug 18, 2023
2 parents 48c627d + a6c36a9 commit 7f5c03c
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions core/src/main/java/io/confluent/rest/FileWatcher.java
Expand Up @@ -31,19 +31,10 @@

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

// reference https://gist.github.com/danielflower/f54c2fe42d32356301c68860a4ab21ed
public class FileWatcher implements Runnable {
private static final Logger log = LoggerFactory.getLogger(FileWatcher.class);
private static final ExecutorService executor = Executors.newFixedThreadPool(1,
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
});

public interface Callback {
void run() throws Exception;
Expand All @@ -53,6 +44,12 @@ public interface Callback {
private final WatchService watchService;
private final Path file;
private final Callback callback;
private final ExecutorService executorService = Executors.newSingleThreadExecutor(
r -> {
Thread thread = new Thread(r, "file-watcher");
thread.setDaemon(true);
return thread;
});

public FileWatcher(Path file, Callback callback) throws IOException {
this.file = file;
Expand All @@ -75,10 +72,11 @@ public FileWatcher(Path file, Callback callback) throws IOException {
public static void onFileChange(Path file, Callback callback) throws IOException {
log.info("Constructing a new watch service: " + file);
FileWatcher fileWatcher = new FileWatcher(file, callback);
executor.submit(fileWatcher);
fileWatcher.executorService.submit(fileWatcher);
}

public void run() {
log.info("Running file watcher service thread");
try {
while (!shutdown) {
try {
Expand Down Expand Up @@ -147,6 +145,7 @@ public void shutdown() {
shutdown = true;
try {
watchService.close();
executorService.shutdown();
} catch (IOException e) {
log.info("Error closing watch service", e);
}
Expand Down

0 comments on commit 7f5c03c

Please sign in to comment.