Commit 6967ddd5 by Johannes Edmeier

Make StatusUpdater more robust

Check if the status field is a String and don't propagate any Exception from the StatusUpdater, so that the application don't fail like in #120
parent 47262852
...@@ -22,7 +22,6 @@ import org.slf4j.LoggerFactory; ...@@ -22,7 +22,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent; import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
...@@ -34,7 +33,7 @@ import de.codecentric.boot.admin.registry.store.ApplicationStore; ...@@ -34,7 +33,7 @@ import de.codecentric.boot.admin.registry.store.ApplicationStore;
* The StatusUpdater is responsible for updatig the status of all or a single * The StatusUpdater is responsible for updatig the status of all or a single
* application querying the healthUrl. * application querying the healthUrl.
* *
* @author Johannes Stelzer * @author Johannes Edmeier
* *
*/ */
public class StatusUpdater implements ApplicationEventPublisherAware { public class StatusUpdater implements ApplicationEventPublisherAware {
...@@ -50,7 +49,6 @@ public class StatusUpdater implements ApplicationEventPublisherAware { ...@@ -50,7 +49,6 @@ public class StatusUpdater implements ApplicationEventPublisherAware {
*/ */
private long statusLifetime = 30_000L; private long statusLifetime = 30_000L;
public StatusUpdater(RestTemplate restTemplate, ApplicationStore store) { public StatusUpdater(RestTemplate restTemplate, ApplicationStore store) {
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
this.store = store; this.store = store;
...@@ -59,7 +57,7 @@ public class StatusUpdater implements ApplicationEventPublisherAware { ...@@ -59,7 +57,7 @@ public class StatusUpdater implements ApplicationEventPublisherAware {
public void updateStatusForAllApplications() { public void updateStatusForAllApplications() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
for (Application application : store.findAll()) { for (Application application : store.findAll()) {
if ( now - statusLifetime > application.getStatusInfo().getTimestamp()) { if (now - statusLifetime > application.getStatusInfo().getTimestamp()) {
updateStatus(application); updateStatus(application);
} }
} }
...@@ -85,18 +83,18 @@ public class StatusUpdater implements ApplicationEventPublisherAware { ...@@ -85,18 +83,18 @@ public class StatusUpdater implements ApplicationEventPublisherAware {
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ResponseEntity<Map<String, String>> response = restTemplate.getForEntity(application.getHealthUrl(), (Class<Map<String, String>>)(Class<?>) Map.class); ResponseEntity<Map<String, Object>> response = restTemplate.getForEntity(application.getHealthUrl(), (Class<Map<String, Object>>)(Class<?>) Map.class);
LOGGER.debug("/health for {} responded with {}", application, response); LOGGER.debug("/health for {} responded with {}", application, response);
if (response.hasBody() && response.getBody().get("status") != null ) { if (response.hasBody() && response.getBody().get("status") instanceof String) {
return StatusInfo.valueOf(response.getBody().get("status")); return StatusInfo.valueOf((String) response.getBody().get("status"));
} else if (response.getStatusCode().is2xxSuccessful()) { } else if (response.getStatusCode().is2xxSuccessful()) {
return StatusInfo.ofUp(); return StatusInfo.ofUp();
} else { } else {
return StatusInfo.ofDown(); return StatusInfo.ofDown();
} }
} catch (RestClientException ex){ } catch (Exception ex){
LOGGER.warn("Couldn't retrieve status for {}", application, ex); LOGGER.warn("Couldn't retrieve status for {}", application, ex);
return StatusInfo.ofOffline(); return StatusInfo.ofOffline();
} }
...@@ -111,5 +109,4 @@ public class StatusUpdater implements ApplicationEventPublisherAware { ...@@ -111,5 +109,4 @@ public class StatusUpdater implements ApplicationEventPublisherAware {
this.publisher = publisher; this.publisher = publisher;
} }
} }
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