Commit c15b3bbc by Johannes Edmeier

Change status update to be executed async

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