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