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,13 +26,17 @@ public class StatusUpdateApplicationListener {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
if (event.getApplicationContext().getParent() == null) {
startStatusUpdate();
}
}
@EventListener
public void onContextClosed(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
stopStatusUpdate();
}
}
@EventListener
public void onClientApplicationRegistered(ClientApplicationRegisteredEvent event) {
......
......@@ -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,11 +73,13 @@ public class AdminClientProperties {
@EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
serverPort = event.getApplicationContext().getEnvironment().getProperty("local.server.port",
Integer.class);
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() {
if (managementUrl != null) {
......
......@@ -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,12 +58,14 @@ public class RegistrationApplicationListener {
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void onClosedContext(ContextClosedEvent event) {
if (event.getApplicationContext().getParent() == null) {
stopRegisterTask();
if (autoDeregister) {
registrator.deregister();
}
}
}
public void startRegisterTask() {
if (scheduledTask != null && !scheduledTask.isDone()) {
......
......@@ -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