Skip to content

Commit

Permalink
Issue #6020 Use ScheduledExecutorScheduler for Scanner
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Mar 9, 2021
1 parent 00f40f7 commit ca6e406
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Expand Up @@ -29,7 +29,9 @@
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.eclipse.jetty.util.IncludeExcludeSet;
import org.eclipse.jetty.util.Scanner;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.webapp.WebAppContext;

/**
Expand Down Expand Up @@ -155,6 +157,11 @@ private void startScanner()
scanner.setReportExistingFilesOnStartup(false);
configureScanner();
getLog().info("Scan interval sec = " + scan);

//unmanage scheduler so it is not stopped with the scanner
Scheduler scheduler = scanner.getBean(Scheduler.class);
scanner.unmanage(scheduler);
LifeCycle.start(scheduler);
scanner.start();
}
else
Expand Down
27 changes: 14 additions & 13 deletions jetty-util/src/main/java/org/eclipse/jetty/util/Scanner.java
Expand Up @@ -36,9 +36,9 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;

import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.util.thread.TimerScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -48,7 +48,7 @@
* Utility for scanning a directory for added, removed and changed
* files and reporting these events via registered Listeners.
*/
public class Scanner extends AbstractLifeCycle
public class Scanner extends ContainerLifeCycle
{
/**
* When walking a directory, a depth of 1 ensures that
Expand All @@ -69,7 +69,7 @@ public class Scanner extends AbstractLifeCycle
private boolean _reportExisting = true;
private boolean _reportDirs = true;
private Scheduler.Task _task;
private Scheduler _scheduler;
private final Scheduler _scheduler;
private int _scanDepth = DEFAULT_SCAN_DEPTH;

private enum Status
Expand Down Expand Up @@ -279,9 +279,17 @@ default void scanEnded(int cycle) throws Exception
{
}
}

public Scanner()
{
this(new ScheduledExecutorScheduler("Scanner-" + SCANNER_IDS.getAndIncrement(), true, 1));
}

public Scanner(Scheduler scheduler)
{
//Create the scheduler and start it
_scheduler = scheduler;
addBean(_scheduler);
}

/**
Expand Down Expand Up @@ -514,10 +522,7 @@ public void doStart() throws Exception
_prevScan = scanFiles();
}


//Create the scheduler and start it
_scheduler = new TimerScheduler("Scanner-" + SCANNER_IDS.getAndIncrement(), true);
_scheduler.start();
super.doStart();

//schedule the scan
schedule();
Expand All @@ -539,10 +544,6 @@ public void doStop() throws Exception
_task = null;
if (task != null)
task.cancel();
Scheduler scheduler = _scheduler;
_scheduler = null;
if (scheduler != null)
scheduler.stop();
}

/**
Expand Down

0 comments on commit ca6e406

Please sign in to comment.