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.

View File

@ -23,8 +23,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.devtools.restart.RestartApplicationListener;
import org.springframework.boot.devtools.restart.Restarter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.test.util.ReflectionTestUtils;
@ -42,10 +40,15 @@ import static org.mockito.Mockito.mock;
*/
public class RestartApplicationListenerTests {
private static final String ENABLED_PROPERTY = "spring.devtools.restart.enabled";
private static final String[] ARGS = new String[] { "a", "b", "c" };
@Before
@After
public void cleanup() {
Restarter.clearInstance();
System.clearProperty(ENABLED_PROPERTY);
}
@Test
@ -57,11 +60,25 @@ public class RestartApplicationListenerTests {
@Test
public void initializeWithReady() throws Exception {
testInitialize(false);
assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "args"),
equalTo((Object) ARGS));
assertThat(Restarter.getInstance().isFinished(), equalTo(true));
}
@Test
public void initializeWithFail() throws Exception {
testInitialize(true);
assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "args"),
equalTo((Object) ARGS));
assertThat(Restarter.getInstance().isFinished(), equalTo(true));
}
@Test
public void disableWithSystemProperty() throws Exception {
System.setProperty(ENABLED_PROPERTY, "false");
testInitialize(false);
assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "enabled"),
equalTo((Object) false));
}
private void testInitialize(boolean failed) {
@ -69,21 +86,17 @@ public class RestartApplicationListenerTests {
RestartApplicationListener listener = new RestartApplicationListener();
SpringApplication application = new SpringApplication();
ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
String[] args = new String[] { "a", "b", "c" };
listener.onApplicationEvent(new ApplicationStartedEvent(application, args));
listener.onApplicationEvent(new ApplicationStartedEvent(application, ARGS));
assertThat(Restarter.getInstance(), not(nullValue()));
assertThat(Restarter.getInstance().isFinished(), equalTo(false));
assertThat(ReflectionTestUtils.getField(Restarter.getInstance(), "args"),
equalTo((Object) args));
if (failed) {
listener.onApplicationEvent(new ApplicationFailedEvent(application, args,
listener.onApplicationEvent(new ApplicationFailedEvent(application, ARGS,
context, new RuntimeException()));
}
else {
listener.onApplicationEvent(new ApplicationReadyEvent(application, args,
listener.onApplicationEvent(new ApplicationReadyEvent(application, ARGS,
context));
}
assertThat(Restarter.getInstance().isFinished(), equalTo(true));
}
}