Merge branch 'gh-4311'

pull/4304/merge
Andy Wilkinson 9 years ago
commit 783a59f638

@ -337,11 +337,15 @@ public class LoggingApplicationListener implements GenericApplicationListener {
Runnable shutdownHandler = loggingSystem.getShutdownHandler(); Runnable shutdownHandler = loggingSystem.getShutdownHandler();
if (shutdownHandler != null if (shutdownHandler != null
&& shutdownHookRegistered.compareAndSet(false, true)) { && shutdownHookRegistered.compareAndSet(false, true)) {
Runtime.getRuntime().addShutdownHook(new Thread()); registerShutdownHook(new Thread(shutdownHandler));
} }
} }
} }
void registerShutdownHook(Thread shutdownHook) {
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
public void setOrder(int order) { public void setOrder(int order) {
this.order = order; this.order = order;
} }

@ -18,6 +18,8 @@ package org.springframework.boot.logging;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.LogManager; import java.util.logging.LogManager;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -44,6 +46,7 @@ import org.springframework.context.support.GenericApplicationContext;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -346,26 +349,29 @@ public class LoggingApplicationListenerTests {
@Test @Test
public void shutdownHookIsNotRegisteredByDefault() throws Exception { public void shutdownHookIsNotRegisteredByDefault() throws Exception {
TestLoggingApplicationListener listener = new TestLoggingApplicationListener();
System.setProperty(LoggingSystem.class.getName(), System.setProperty(LoggingSystem.class.getName(),
NullShutdownHandlerLoggingSystem.class.getName()); TestShutdownHandlerLoggingSystem.class.getName());
this.initializer.onApplicationEvent( listener.onApplicationEvent(
new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); new ApplicationStartedEvent(new SpringApplication(), NO_ARGS));
this.initializer.initialize(this.context.getEnvironment(), listener.initialize(this.context.getEnvironment(), this.context.getClassLoader());
this.context.getClassLoader()); assertThat(listener.shutdownHook, is(nullValue()));
assertThat(NullShutdownHandlerLoggingSystem.shutdownHandlerRequested, is(false));
} }
@Test @Test
public void shutdownHookCanBeRegistered() throws Exception { public void shutdownHookCanBeRegistered() throws Exception {
TestLoggingApplicationListener listener = new TestLoggingApplicationListener();
System.setProperty(LoggingSystem.class.getName(), System.setProperty(LoggingSystem.class.getName(),
NullShutdownHandlerLoggingSystem.class.getName()); TestShutdownHandlerLoggingSystem.class.getName());
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"logging.register_shutdown_hook:true"); "logging.register_shutdown_hook:true");
this.initializer.onApplicationEvent( listener.onApplicationEvent(
new ApplicationStartedEvent(new SpringApplication(), NO_ARGS)); new ApplicationStartedEvent(new SpringApplication(), NO_ARGS));
this.initializer.initialize(this.context.getEnvironment(), listener.initialize(this.context.getEnvironment(), this.context.getClassLoader());
this.context.getClassLoader()); assertThat(listener.shutdownHook, is(not(nullValue())));
assertThat(NullShutdownHandlerLoggingSystem.shutdownHandlerRequested, is(true)); listener.shutdownHook.start();
assertThat(TestShutdownHandlerLoggingSystem.shutdownLatch.await(30,
TimeUnit.SECONDS), is(true));
} }
private boolean bridgeHandlerInstalled() { private boolean bridgeHandlerInstalled() {
@ -379,12 +385,13 @@ public class LoggingApplicationListenerTests {
return false; return false;
} }
public static class NullShutdownHandlerLoggingSystem extends AbstractLoggingSystem { public static class TestShutdownHandlerLoggingSystem extends AbstractLoggingSystem {
static boolean shutdownHandlerRequested = false; private static CountDownLatch shutdownLatch;
public NullShutdownHandlerLoggingSystem(ClassLoader classLoader) { public TestShutdownHandlerLoggingSystem(ClassLoader classLoader) {
super(classLoader); super(classLoader);
TestShutdownHandlerLoggingSystem.shutdownLatch = new CountDownLatch(1);
} }
@Override @Override
@ -410,8 +417,26 @@ public class LoggingApplicationListenerTests {
@Override @Override
public Runnable getShutdownHandler() { public Runnable getShutdownHandler() {
shutdownHandlerRequested = true; return new Runnable() {
return null;
@Override
public void run() {
TestShutdownHandlerLoggingSystem.shutdownLatch.countDown();
}
};
}
}
public static class TestLoggingApplicationListener
extends LoggingApplicationListener {
private Thread shutdownHook;
@Override
void registerShutdownHook(Thread shutdownHook) {
this.shutdownHook = shutdownHook;
} }
} }

Loading…
Cancel
Save