Provide a way to disable the Restarter

Allow the Restarter to be disabled via a System property.

Fixes gh-3173
This commit is contained in:
Phillip Webb
2015-06-10 13:10:00 -07:00
parent b57802190d
commit 88c693c6e1
3 changed files with 61 additions and 10 deletions

View File

@@ -35,10 +35,12 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
private int order = HIGHEST_PRECEDENCE;
private static final String ENABLED_PROPERTY = "spring.devtools.restart.enabled";
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationStartedEvent) {
Restarter.initialize(((ApplicationStartedEvent) event).getArgs());
onApplicationStartedEvent((ApplicationStartedEvent) event);
}
if (event instanceof ApplicationReadyEvent
|| event instanceof ApplicationFailedEvent) {
@@ -46,6 +48,18 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
}
}
private void onApplicationStartedEvent(ApplicationStartedEvent event) {
// It's too early to use the Spring environment but we should still allow
// users to disable restart using a System property.
String enabled = System.getProperty(ENABLED_PROPERTY);
if (enabled == null || Boolean.parseBoolean(enabled)) {
Restarter.initialize(event.getArgs());
}
else {
Restarter.disable();
}
}
@Override
public int getOrder() {
return this.order;

View File

@@ -78,12 +78,16 @@ import org.springframework.util.ReflectionUtils;
*/
public class Restarter {
private static final String[] NO_ARGS = {};
private static Restarter instance;
private Log logger = new DeferredLog();
private final boolean forceReferenceCleanup;
private boolean enabled = true;
private URL[] initialUrls;
private final String mainClassName;
@@ -185,6 +189,14 @@ public class Restarter {
}
}
/**
* Set if restart support is enabled.
* @param enabled if restart support is enabled
*/
private void setEnabled(boolean enabled) {
this.enabled = false;
}
/**
* Add additional URLs to be includes in the next restart.
* @param urls the urls to add
@@ -215,6 +227,10 @@ public class Restarter {
* Restart the running application.
*/
public void restart() {
if (!this.enabled) {
this.logger.debug("Application restart is disabled");
return;
}
this.logger.debug("Restarting application");
getLeakSafeThread().call(new Callable<Void>() {
@@ -402,6 +418,14 @@ public class Restarter {
return this.initialUrls;
}
/**
* Initialize and disable restart support.
*/
public static void disable() {
initialize(NO_ARGS, false, RestartInitializer.NONE);
getInstance().setEnabled(false);
}
/**
* Initialize restart support. See
* {@link #initialize(String[], boolean, RestartInitializer)} for details.