Extract AbstractHealthAggregator
This commit makes it easier for users to implement HealthAggregators
This commit is contained in:
parent
f9aeb6aefe
commit
40b55b0ff6
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-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 org.springframework.boot.actuate.health;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base {@link HealthAggregator} implementation to allow subclasses to focus on
|
||||||
|
* aggregating the {@link Status} instances and not deal with contextual details etc.
|
||||||
|
*
|
||||||
|
* @author Christian Dupuis
|
||||||
|
* @since 1.1.0
|
||||||
|
*/
|
||||||
|
public abstract class AbstractHealthAggregator implements HealthAggregator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final Health aggregate(Map<String, Health> healths) {
|
||||||
|
Health health = new Health();
|
||||||
|
|
||||||
|
List<Status> status = new ArrayList<Status>();
|
||||||
|
for (Map.Entry<String, Health> h : healths.entrySet()) {
|
||||||
|
health.withDetail(h.getKey(), h.getValue());
|
||||||
|
status.add(h.getValue().getStatus());
|
||||||
|
}
|
||||||
|
health.status(aggregateStatus(status));
|
||||||
|
return health;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Status aggregateStatus(List<Status> status);
|
||||||
|
}
|
||||||
|
|
@ -16,12 +16,10 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.health;
|
package org.springframework.boot.actuate.health;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default {@link HealthAggregator} implementation that aggregates {@link Health}
|
* Default {@link HealthAggregator} implementation that aggregates {@link Health}
|
||||||
|
|
@ -34,48 +32,41 @@ import java.util.Map;
|
||||||
* @author Christian Dupuis
|
* @author Christian Dupuis
|
||||||
* @since 1.1.0
|
* @since 1.1.0
|
||||||
*/
|
*/
|
||||||
public class OrderedHealthAggregator implements HealthAggregator {
|
public class OrderedHealthAggregator extends AbstractHealthAggregator {
|
||||||
|
|
||||||
private List<String> statusOrder = Arrays.asList("DOWN", "OUT_OF_SERVICE", "UP",
|
private List<String> statusOrder = Arrays.asList("DOWN", "OUT_OF_SERVICE", "UP",
|
||||||
"UNKOWN");
|
"UNKOWN");
|
||||||
|
|
||||||
@Override
|
|
||||||
public Health aggregate(Map<String, Health> healths) {
|
|
||||||
Health health = new Health();
|
|
||||||
|
|
||||||
List<Status> status = new ArrayList<Status>();
|
|
||||||
for (Map.Entry<String, Health> h : healths.entrySet()) {
|
|
||||||
health.withDetail(h.getKey(), h.getValue());
|
|
||||||
status.add(h.getValue().getStatus());
|
|
||||||
}
|
|
||||||
health.status(aggregateStatus(status));
|
|
||||||
return health;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStatusOrder(List<String> statusOrder) {
|
public void setStatusOrder(List<String> statusOrder) {
|
||||||
this.statusOrder = statusOrder;
|
this.statusOrder = statusOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected Status aggregateStatus(List<Status> status) {
|
protected Status aggregateStatus(List<Status> status) {
|
||||||
|
// If no status is given return UNKOWN
|
||||||
if (status.size() == 0) {
|
if (status.size() == 0) {
|
||||||
return Status.UNKOWN;
|
return Status.UNKOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.sort(status, new Comparator<Status>() {
|
// Sort given Status instances by configured order
|
||||||
|
Collections.sort(status, new StatusComparator(this.statusOrder));
|
||||||
@Override
|
|
||||||
public int compare(Status s1, Status s2) {
|
|
||||||
return Integer.valueOf(
|
|
||||||
OrderedHealthAggregator.this.statusOrder.indexOf(s1.getCode()))
|
|
||||||
.compareTo(
|
|
||||||
Integer.valueOf(OrderedHealthAggregator.this.statusOrder
|
|
||||||
.indexOf(s2.getCode())));
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return status.get(0);
|
return status.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class StatusComparator implements Comparator<Status> {
|
||||||
|
|
||||||
|
private final List<String> statusOrder;
|
||||||
|
|
||||||
|
public StatusComparator(List<String> statusOrder) {
|
||||||
|
this.statusOrder = statusOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(Status s1, Status s2) {
|
||||||
|
return Integer.valueOf(this.statusOrder.indexOf(s1.getCode())).compareTo(
|
||||||
|
Integer.valueOf(this.statusOrder.indexOf(s2.getCode())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue