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

add hot reload functionnality for mock server #909

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
change file change handler to use Jave Watchservice instead of a slee…
…p loop
benjaminqc committed Oct 7, 2019

Partially verified

This commit is signed with the committer’s verified signature.
spydon’s contribution has been verified via GPG key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
commit 9abcaf49178a0bd54c8c63518cde1b3327aa417f
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.intuit.karate.netty;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileChangedWatcher {

private static final Logger logger = LoggerFactory.getLogger(FileChangedWatcher.class);

private File file;
private FeatureServer server;
private Integer port;
@@ -20,19 +31,24 @@ public FileChangedWatcher(File mock, FeatureServer server, Integer port, boolean
this.key = key;
}

public void watch() throws InterruptedException {

long currentModifiedDate = file.lastModified();

while (true) {

long newModifiedDate = file.lastModified();

if (newModifiedDate != currentModifiedDate) {
currentModifiedDate = newModifiedDate;
onModified();
public void watch() throws InterruptedException, IOException {

try {
final Path directoryPath = file.toPath().getParent();
final WatchService watchService = FileSystems.getDefault().newWatchService();
directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
final WatchKey wk = watchService.take();
for (WatchEvent<?> event : wk.pollEvents()) {
final Path fileChangedPath = (Path) event.context();
if (fileChangedPath.endsWith(file.getName())) {
onModified();
}
}
wk.reset();
}
Thread.sleep(500);
} catch (Exception exception) {
logger.error("exception when handling change of mock file");
}
}