StatusUpdater.java 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.codecentric.boot.admin.registry;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import de.codecentric.boot.admin.registry.store.ApplicationStore;

/**
33 34
 * The StatusUpdater is responsible for updatig the status of all or a single application querying
 * the healthUrl.
35
 *
36
 * @author Johannes Edmeier
37 38 39 40 41 42 43 44 45
 */
public class StatusUpdater implements ApplicationEventPublisherAware {
	private static final Logger LOGGER = LoggerFactory.getLogger(StatusUpdater.class);

	private final ApplicationStore store;
	private final RestTemplate restTemplate;
	private ApplicationEventPublisher publisher;

	/**
46
	 * Lifetime of status in ms. The status won't be updated as long the last status isn't expired.
47
	 */
48
	private long statusLifetime = 10_000L;
49 50 51 52 53 54 55 56 57

	public StatusUpdater(RestTemplate restTemplate, ApplicationStore store) {
		this.restTemplate = restTemplate;
		this.store = store;
	}

	public void updateStatusForAllApplications() {
		long now = System.currentTimeMillis();
		for (Application application : store.findAll()) {
58
			if (now - statusLifetime > application.getStatusInfo().getTimestamp()) {
59 60 61 62 63 64 65 66 67
				updateStatus(application);
			}
		}
	}

	public void updateStatus(Application application) {
		StatusInfo oldStatus = application.getStatusInfo();
		StatusInfo newStatus = queryStatus(application);

68
		Application newState = Application.create(application).withStatusInfo(newStatus).build();
69 70 71
		store.save(newState);

		if (!newStatus.equals(oldStatus)) {
72 73
			publisher.publishEvent(
					new ClientApplicationStatusChangedEvent(newState, oldStatus, newStatus));
74 75 76 77 78 79 80 81
		}
	}

	private StatusInfo queryStatus(Application application) {
		LOGGER.trace("Updating status for {}", application);

		try {
			@SuppressWarnings("unchecked")
82 83
			ResponseEntity<Map<String, Object>> response = restTemplate.getForEntity(
					application.getHealthUrl(), (Class<Map<String, Object>>) (Class<?>) Map.class);
84 85
			LOGGER.debug("/health for {} responded with {}", application, response);

86 87
			if (response.hasBody() && response.getBody().get("status") instanceof String) {
				return StatusInfo.valueOf((String) response.getBody().get("status"));
88 89 90 91 92 93
			} else if (response.getStatusCode().is2xxSuccessful()) {
				return StatusInfo.ofUp();
			} else {
				return StatusInfo.ofDown();
			}

94
		} catch (Exception ex) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
			LOGGER.warn("Couldn't retrieve status for {}", application, ex);
			return StatusInfo.ofOffline();
		}
	}

	public void setStatusLifetime(long statusLifetime) {
		this.statusLifetime = statusLifetime;
	}

	@Override
	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
		this.publisher = publisher;
	}

}