Commit e7fd7a53 by Johannes Stelzer

Execute registration on startup asynchronously to not block startup

parent 0bd7eee3
......@@ -9,27 +9,39 @@ import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import de.codecentric.boot.admin.config.AdminProperties;
@Order(Ordered.LOWEST_PRECEDENCE)
public class RegistrationApplicationListener implements
ApplicationListener<ApplicationEvent> {
public class RegistrationApplicationListener implements ApplicationListener<ApplicationEvent> {
private final AdminProperties admin;
private final ApplicationRegistrator registrator;
private final TaskExecutor executor;
public RegistrationApplicationListener(AdminProperties admin,
ApplicationRegistrator registrator) {
ApplicationRegistrator registrator, TaskExecutor executor) {
this.admin = admin;
this.registrator = registrator;
this.executor = executor;
}
public RegistrationApplicationListener(AdminProperties admin,
ApplicationRegistrator registrator) {
this(admin, registrator, new SimpleAsyncTaskExecutor());
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (startedDeployedWar(event) || startedEmbeddedServer(event)) {
executor.execute(new Runnable() {
@Override
public void run() {
registrator.register();
}
else if (admin.isAutoDeregistration()
&& event instanceof ContextClosedEvent) {
});
} else if (admin.isAutoDeregistration() && event instanceof ContextClosedEvent) {
registrator.deregister();
}
}
......@@ -50,4 +62,3 @@ ApplicationListener<ApplicationEvent> {
return false;
}
}
......@@ -12,6 +12,7 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerInitial
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.task.SyncTaskExecutor;
import de.codecentric.boot.admin.config.AdminProperties;
......@@ -27,12 +28,11 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_register_embedded() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(
admin, registrator);
RegistrationApplicationListener listener = new RegistrationApplicationListener(admin,
registrator, new SyncTaskExecutor());
listener.onApplicationEvent(new EmbeddedServletContainerInitializedEvent(
mock(EmbeddedWebApplicationContext.class),
mock(EmbeddedServletContainer.class)));
mock(EmbeddedWebApplicationContext.class), mock(EmbeddedServletContainer.class)));
verify(registrator).register();
}
......@@ -40,11 +40,11 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_register_war() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(
admin, registrator);
RegistrationApplicationListener listener = new RegistrationApplicationListener(admin,
registrator, new SyncTaskExecutor());
listener.onApplicationEvent(new ContextRefreshedEvent(
mock(EmbeddedWebApplicationContext.class)));
listener.onApplicationEvent(
new ContextRefreshedEvent(mock(EmbeddedWebApplicationContext.class)));
verify(registrator).register();
}
......@@ -52,12 +52,12 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_no_register_war() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(
admin, registrator);
RegistrationApplicationListener listener = new RegistrationApplicationListener(admin,
registrator, new SyncTaskExecutor());
EmbeddedWebApplicationContext context = mock(EmbeddedWebApplicationContext.class);
when(context.getEmbeddedServletContainer()).thenReturn(
mock(EmbeddedServletContainer.class));
when(context.getEmbeddedServletContainer())
.thenReturn(mock(EmbeddedServletContainer.class));
listener.onApplicationEvent(new ContextRefreshedEvent(context));
verify(registrator, never()).register();
......@@ -66,11 +66,11 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_no_deregister() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(
admin, registrator);
RegistrationApplicationListener listener = new RegistrationApplicationListener(admin,
registrator, new SyncTaskExecutor());
listener.onApplicationEvent(new ContextClosedEvent(
mock(EmbeddedWebApplicationContext.class)));
listener.onApplicationEvent(
new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
verify(registrator, never()).deregister();
}
......@@ -78,13 +78,13 @@ public class RegistrationApplicationListenerTest {
@Test
public void test_deregister() {
ApplicationRegistrator registrator = mock(ApplicationRegistrator.class);
RegistrationApplicationListener listener = new RegistrationApplicationListener(
admin, registrator);
RegistrationApplicationListener listener = new RegistrationApplicationListener(admin,
registrator);
admin.setAutoDeregistration(true);
listener.onApplicationEvent(new ContextClosedEvent(
mock(EmbeddedWebApplicationContext.class)));
listener.onApplicationEvent(
new ContextClosedEvent(mock(EmbeddedWebApplicationContext.class)));
verify(registrator).deregister();
}
......
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