Commit ba7cc524 by Johannes Edmeier

Deregister when the context has a parent bootstrap context

fixes #738
parent 92a3442d
...@@ -72,7 +72,8 @@ public class RegistrationApplicationListener implements InitializingBean, Dispos ...@@ -72,7 +72,8 @@ public class RegistrationApplicationListener implements InitializingBean, Dispos
@EventListener @EventListener
@Order(Ordered.LOWEST_PRECEDENCE) @Order(Ordered.LOWEST_PRECEDENCE)
public void onClosedContext(ContextClosedEvent event) { public void onClosedContext(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) { if (event.getApplicationContext().getParent() == null ||
"bootstrap".equals(event.getApplicationContext().getParent().getId())) {
stopRegisterTask(); stopRegisterTask();
if (autoDeregister) { if (autoDeregister) {
......
...@@ -21,6 +21,7 @@ import java.util.concurrent.ScheduledFuture; ...@@ -21,6 +21,7 @@ import java.util.concurrent.ScheduledFuture;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.ConfigurableWebApplicationContext;
...@@ -112,12 +113,42 @@ public class RegistrationApplicationListenerTest { ...@@ -112,12 +113,42 @@ public class RegistrationApplicationListenerTest {
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler); RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
listener.setAutoDeregister(true); listener.setAutoDeregister(true);
listener.onClosedContext(new ContextClosedEvent(mock(WebApplicationContext.class))); listener.onClosedContext(new ContextClosedEvent(mock(ApplicationContext.class)));
verify(registrator).deregister();
}
@Test
public void should_deregister_when_autoDeregister_and_parent_is_bootstrap_contex() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
listener.setAutoDeregister(true);
ApplicationContext parentContext = mock(ApplicationContext.class);
when(parentContext.getId()).thenReturn("bootstrap");
ApplicationContext mockContext = mock(ApplicationContext.class);
when(mockContext.getParent()).thenReturn(parentContext);
listener.onClosedContext(new ContextClosedEvent(mockContext));
verify(registrator).deregister(); verify(registrator).deregister();
} }
@Test @Test
public void should_not_deregister_when_autoDeregister_and_not_root() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
listener.setAutoDeregister(true);
ApplicationContext mockContext = mock(ApplicationContext.class);
when(mockContext.getParent()).thenReturn(mock(ApplicationContext.class));
listener.onClosedContext(new ContextClosedEvent(mockContext));
verify(registrator, never()).deregister();
}
@Test
public void should_init_and_shutdown_taskScheduler() { public void should_init_and_shutdown_taskScheduler() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class); ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class); ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment