Commit 0e9581fd by Johannes Edmeier

Ignore events from child contexts

When includeing spring-cloud-netflix-hystrix-amqp to your application it looks like that the ApplicationReadyEvent is fired twice (from main and child application context). So we need to take care that only the events from the main applications are taken into account. fixes #260
parent c9c67ccb
......@@ -26,12 +26,16 @@ public class StatusUpdateApplicationListener {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
startStatusUpdate();
if (event.getApplicationContext().getParent() == null) {
startStatusUpdate();
}
}
@EventListener
public void onContextClosed(ContextClosedEvent event) {
stopStatusUpdate();
if (event.getApplicationContext().getParent() == null) {
stopStatusUpdate();
}
}
@EventListener
......
......@@ -12,6 +12,7 @@ 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;
......@@ -32,7 +33,8 @@ public class StatusUpdateApplicationListenerTest {
when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
listener.onContextClosed(new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
......
......@@ -73,10 +73,12 @@ public class AdminClientProperties {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
serverPort = event.getApplicationContext().getEnvironment().getProperty("local.server.port",
Integer.class);
managementPort = event.getApplicationContext().getEnvironment()
.getProperty("local.management.port", Integer.class, serverPort);
if (event.getApplicationContext().getParent() == null) {
serverPort = event.getApplicationContext().getEnvironment()
.getProperty("local.server.port", Integer.class);
managementPort = event.getApplicationContext().getEnvironment()
.getProperty("local.management.port", Integer.class, serverPort);
}
}
public String getManagementUrl() {
......
......@@ -50,7 +50,7 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onApplicationReady(ApplicationReadyEvent event) {
if (autoRegister) {
if (event.getApplicationContext().getParent() == null && autoRegister) {
startRegisterTask();
}
}
......@@ -58,10 +58,12 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onClosedContext(ContextClosedEvent event) {
stopRegisterTask();
if (event.getApplicationContext().getParent() == null) {
stopRegisterTask();
if (autoDeregister) {
registrator.deregister();
if (autoDeregister) {
registrator.deregister();
}
}
}
......
......@@ -13,6 +13,7 @@ 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;
......@@ -26,7 +27,8 @@ public class RegistrationApplicationListenerTest {
scheduler);
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
verify(scheduler).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
}
......@@ -40,7 +42,8 @@ public class RegistrationApplicationListenerTest {
listener.setAutoRegister(false);
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.class)));
verify(scheduler, never()).scheduleAtFixedRate(isA(Runnable.class), eq(10_000L));
}
......@@ -57,7 +60,8 @@ public class RegistrationApplicationListenerTest {
when(scheduler.scheduleAtFixedRate(isA(Runnable.class), eq(10_000L))).thenReturn(task);
listener.onApplicationReady(
new ApplicationReadyEvent(mock(SpringApplication.class), null, null));
new ApplicationReadyEvent(mock(SpringApplication.class), null,
mock(ConfigurableApplicationContext.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