diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ShutdownEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ShutdownEndpoint.java index 2b78563cf55..b3009442262 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ShutdownEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ShutdownEndpoint.java @@ -30,6 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext; * * @author Dave Syer * @author Christian Dupuis + * @author Andy Wilkinson */ @ConfigurationProperties(prefix = "endpoints.shutdown") public class ShutdownEndpoint extends AbstractEndpoint> @@ -58,7 +59,7 @@ public class ShutdownEndpoint extends AbstractEndpoint> } finally { - new Thread(new Runnable() { + Thread thread = new Thread(new Runnable() { @Override public void run() { try { @@ -69,8 +70,9 @@ public class ShutdownEndpoint extends AbstractEndpoint> } ShutdownEndpoint.this.context.close(); } - }).start(); - + }); + thread.setContextClassLoader(getClass().getClassLoader()); + thread.start(); } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownEndpointTests.java index a4de92086c9..b6385ae9417 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,9 @@ package org.springframework.boot.actuate.endpoint; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -28,6 +31,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextClosedEvent; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -37,6 +41,7 @@ import static org.junit.Assert.assertTrue; * * @author Phillip Webb * @author Dave Syer + * @author Andy Wilkinson */ public class ShutdownEndpointTests extends AbstractEndpointTests { @@ -53,18 +58,30 @@ public class ShutdownEndpointTests extends AbstractEndpointTests result; + Thread.currentThread().setContextClassLoader( + new URLClassLoader(new URL[0], getClass().getClassLoader())); + try { + result = getEndpointBean().invoke(); + } + finally { + Thread.currentThread().setContextClassLoader(previousTccl); + } + assertThat((String) result.get("message"), startsWith("Shutting down")); assertTrue(this.context.isActive()); - assertTrue(latch.await(10, TimeUnit.SECONDS)); + assertTrue(config.latch.await(10, TimeUnit.SECONDS)); + assertThat(config.threadContextClassLoader, is(getClass().getClassLoader())); } @Configuration @EnableConfigurationProperties public static class Config { - private CountDownLatch latch = new CountDownLatch(1); + private final CountDownLatch latch = new CountDownLatch(1); + + private volatile ClassLoader threadContextClassLoader; @Bean public ShutdownEndpoint endpoint() { @@ -77,6 +94,8 @@ public class ShutdownEndpointTests extends AbstractEndpointTests() { @Override public void onApplicationEvent(ContextClosedEvent event) { + Config.this.threadContextClassLoader = Thread.currentThread() + .getContextClassLoader(); Config.this.latch.countDown(); } };