Commit c15b3bbc by Johannes Edmeier

Change status update to be executed async

parent fef76a64
...@@ -23,7 +23,6 @@ import org.springframework.context.annotation.Bean; ...@@ -23,7 +23,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.client.DefaultResponseErrorHandler; import org.springframework.web.client.DefaultResponseErrorHandler;
...@@ -89,7 +88,7 @@ public class AdminServerCoreConfiguration { ...@@ -89,7 +88,7 @@ public class AdminServerCoreConfiguration {
@Bean @Bean
@Qualifier("updateTaskScheduler") @Qualifier("updateTaskScheduler")
public TaskScheduler updateTaskScheduler() { public ThreadPoolTaskScheduler updateTaskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(1); taskScheduler.setPoolSize(1);
taskScheduler.setRemoveOnCancelPolicy(true); taskScheduler.setRemoveOnCancelPolicy(true);
...@@ -101,7 +100,7 @@ public class AdminServerCoreConfiguration { ...@@ -101,7 +100,7 @@ public class AdminServerCoreConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
public StatusUpdateApplicationListener statusUpdateApplicationListener( public StatusUpdateApplicationListener statusUpdateApplicationListener(
StatusUpdater statusUpdater, StatusUpdater statusUpdater,
@Qualifier("updateTaskScheduler") TaskScheduler taskScheduler) { @Qualifier("updateTaskScheduler") ThreadPoolTaskScheduler taskScheduler) {
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener( StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(
statusUpdater, taskScheduler); statusUpdater, taskScheduler);
listener.setUpdatePeriod(adminServerProperties.getMonitor().getPeriod()); listener.setUpdatePeriod(adminServerProperties.getMonitor().getPeriod());
......
...@@ -7,20 +7,20 @@ import org.slf4j.LoggerFactory; ...@@ -7,20 +7,20 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent; import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
public class StatusUpdateApplicationListener { public class StatusUpdateApplicationListener {
private static final Logger LOGGER = LoggerFactory.getLogger(StatusUpdateApplicationListener.class); private static final Logger LOGGER = LoggerFactory.getLogger(StatusUpdateApplicationListener.class);
private final TaskScheduler taskScheduler; private final ThreadPoolTaskScheduler taskScheduler;
private final StatusUpdater statusUpdater; private final StatusUpdater statusUpdater;
private long updatePeriod = 10_000L; private long updatePeriod = 10_000L;
private ScheduledFuture<?> scheduledTask; private ScheduledFuture<?> scheduledTask;
public StatusUpdateApplicationListener(StatusUpdater statusUpdater, public StatusUpdateApplicationListener(StatusUpdater statusUpdater,
TaskScheduler taskScheduler) { ThreadPoolTaskScheduler taskScheduler) {
this.statusUpdater = statusUpdater; this.statusUpdater = statusUpdater;
this.taskScheduler = taskScheduler; this.taskScheduler = taskScheduler;
} }
...@@ -40,9 +40,14 @@ public class StatusUpdateApplicationListener { ...@@ -40,9 +40,14 @@ public class StatusUpdateApplicationListener {
} }
@EventListener @EventListener
public void onClientApplicationRegistered(ClientApplicationRegisteredEvent event) { public void onClientApplicationRegistered(final ClientApplicationRegisteredEvent event) {
taskScheduler.submit(new Runnable() {
@Override
public void run() {
statusUpdater.updateStatus(event.getApplication()); statusUpdater.updateStatus(event.getApplication());
} }
});
}
public void startStatusUpdate() { public void startStatusUpdate() {
if (scheduledTask != null && !scheduledTask.isDone()) { if (scheduledTask != null && !scheduledTask.isDone()) {
......
package de.codecentric.boot.admin.registry; package de.codecentric.boot.admin.registry;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA; import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import org.junit.Test; import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.util.concurrent.SettableListenableFuture;
import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.ConfigurableWebApplicationContext;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent; import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
...@@ -25,7 +30,7 @@ public class StatusUpdateApplicationListenerTest { ...@@ -25,7 +30,7 @@ public class StatusUpdateApplicationListenerTest {
@Test @Test
public void test_start_stop() throws Exception { public void test_start_stop() throws Exception {
StatusUpdater statusUpdater = mock(StatusUpdater.class); StatusUpdater statusUpdater = mock(StatusUpdater.class);
TaskScheduler scheduler = mock(TaskScheduler.class); ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener( StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(
statusUpdater, scheduler); statusUpdater, scheduler);
...@@ -44,7 +49,17 @@ public class StatusUpdateApplicationListenerTest { ...@@ -44,7 +49,17 @@ public class StatusUpdateApplicationListenerTest {
@Test @Test
public void test_newApplication() throws Exception { public void test_newApplication() throws Exception {
StatusUpdater statusUpdater = mock(StatusUpdater.class); StatusUpdater statusUpdater = mock(StatusUpdater.class);
TaskScheduler scheduler = mock(TaskScheduler.class); ThreadPoolTaskScheduler scheduler = mock(ThreadPoolTaskScheduler.class);
when(scheduler.submit(any(Runnable.class))).then(new Answer<Future<?>>() {
@Override
public Future<?> answer(InvocationOnMock invocation) throws Throwable {
invocation.getArgumentAt(0, Runnable.class).run();
SettableListenableFuture<?> future = new SettableListenableFuture<Void>();
future.set(null);
return future;
}
});
StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener( StatusUpdateApplicationListener listener = new StatusUpdateApplicationListener(
statusUpdater, scheduler); statusUpdater, scheduler);
......
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