Skip to content

Commit

Permalink
Deregister when the context has a parent bootstrap context
Browse files Browse the repository at this point in the history
fixes #738
  • Loading branch information
joshiste committed May 18, 2018
1 parent 92a3442 commit ba7cc52
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void onApplicationReady(ApplicationReadyEvent event) {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onClosedContext(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
if (event.getApplicationContext().getParent() == null ||
"bootstrap".equals(event.getApplicationContext().getParent().getId())) {
stopRegisterTask();

if (autoDeregister) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.context.ConfigurableWebApplicationContext;
Expand Down Expand Up @@ -112,11 +113,41 @@ public void should_deregister_when_autoDeregister() {
RegistrationApplicationListener listener = new RegistrationApplicationListener(registrator, scheduler);
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();
}

@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() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
Expand Down

0 comments on commit ba7cc52

Please sign in to comment.