Commit 9304e6a4 by Johannes Edmeier

Don't ignore events from child contexts

With the fix for #260 we ignored all events from child contexts which turns out to be wrong since the "main" application context is, in case spring cloud config is used, is a child of the bootstrap context. So we just act on the events from WebApplicationContexts. After they're fired up the needed port information is available. fixes #277
parent 57bae5d1
......@@ -8,6 +8,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.context.WebApplicationContext;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
......@@ -26,14 +27,14 @@ public class StatusUpdateApplicationListener {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
if (event.getApplicationContext().getParent() == null) {
if (event.getApplicationContext() instanceof WebApplicationContext) {
startStatusUpdate();
}
}
@EventListener
public void onContextClosed(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
if (event.getApplicationContext() instanceof WebApplicationContext) {
stopStatusUpdate();
}
}
......
......@@ -12,9 +12,9 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.model.Application;
......@@ -34,7 +34,7 @@ public class StatusUpdateApplicationListenerTest {
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
mock(ConfigurableWebApplicationContext.class)));
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
listener.onContextClosed(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
......
......@@ -26,6 +26,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.event.EventListener;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
@ConfigurationProperties(prefix = "spring.boot.admin.client")
public class AdminClientProperties {
......@@ -73,7 +74,7 @@ public class AdminClientProperties {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
if (event.getApplicationContext().getParent() == null) {
if (event.getApplicationContext() instanceof WebApplicationContext) {
serverPort = event.getApplicationContext().getEnvironment()
.getProperty("local.server.port", Integer.class);
managementPort = event.getApplicationContext().getEnvironment()
......
......@@ -25,6 +25,7 @@ import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.context.WebApplicationContext;
/**
* Listener responsible for starting and stopping the registration task when the application is
......@@ -50,7 +51,7 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onApplicationReady(ApplicationReadyEvent event) {
if (event.getApplicationContext().getParent() == null && autoRegister) {
if (event.getApplicationContext() instanceof WebApplicationContext && autoRegister) {
startRegisterTask();
}
}
......@@ -58,7 +59,7 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onClosedContext(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
if (event.getApplicationContext() instanceof WebApplicationContext) {
stopRegisterTask();
if (autoDeregister) {
......
......@@ -13,9 +13,9 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.context.ConfigurableWebApplicationContext;
public class RegistrationApplicationListenerTest {
......@@ -28,7 +28,7 @@ public class RegistrationApplicationListenerTest {
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
mock(ConfigurableWebApplicationContext.class)));
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
}
......@@ -43,7 +43,7 @@ public class RegistrationApplicationListenerTest {
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
mock(ConfigurableWebApplicationContext.class)));
verify(scheduler, never()).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
}
......@@ -61,7 +61,7 @@ public class RegistrationApplicationListenerTest {
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
mock(ConfigurableWebApplicationContext.class)));
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
......
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