|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.health;
|
18 | 18 |
|
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
19 | 22 | import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
|
| 23 | +import org.springframework.boot.actuate.health.DefaultHealthIndicatorRegistry; |
20 | 24 | import org.springframework.boot.actuate.health.HealthAggregator;
|
21 | 25 | import org.springframework.boot.actuate.health.HealthIndicator;
|
| 26 | +import org.springframework.boot.actuate.health.HealthIndicatorRegistry; |
22 | 27 | import org.springframework.boot.actuate.health.OrderedHealthAggregator;
|
23 | 28 | import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
|
24 | 29 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
25 | 30 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
26 | 31 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
| 32 | +import org.springframework.context.ApplicationContext; |
27 | 33 | import org.springframework.context.annotation.Bean;
|
28 | 34 | import org.springframework.context.annotation.Configuration;
|
| 35 | +import org.springframework.util.ClassUtils; |
29 | 36 |
|
30 | 37 | /**
|
31 | 38 | * {@link EnableAutoConfiguration Auto-configuration} for {@link HealthIndicator}s.
|
32 | 39 | *
|
33 | 40 | * @author Andy Wilkinson
|
34 | 41 | * @author Stephane Nicoll
|
35 | 42 | * @author Phillip Webb
|
| 43 | + * @author Vedran Pavic |
36 | 44 | * @since 2.0.0
|
37 | 45 | */
|
38 | 46 | @Configuration
|
@@ -61,4 +69,34 @@ public OrderedHealthAggregator healthAggregator() {
|
61 | 69 | return healthAggregator;
|
62 | 70 | }
|
63 | 71 |
|
| 72 | + @Bean |
| 73 | + @ConditionalOnMissingBean(HealthIndicatorRegistry.class) |
| 74 | + public HealthIndicatorRegistry healthIndicatorRegistry( |
| 75 | + ApplicationContext applicationContext) { |
| 76 | + HealthIndicatorRegistry registry = new DefaultHealthIndicatorRegistry(); |
| 77 | + Map<String, HealthIndicator> indicators = new LinkedHashMap<>(); |
| 78 | + indicators.putAll(applicationContext.getBeansOfType(HealthIndicator.class)); |
| 79 | + if (ClassUtils.isPresent("reactor.core.publisher.Flux", null)) { |
| 80 | + new ReactiveHealthIndicators().get(applicationContext) |
| 81 | + .forEach(indicators::putIfAbsent); |
| 82 | + } |
| 83 | + indicators.forEach(registry::register); |
| 84 | + return registry; |
| 85 | + } |
| 86 | + |
| 87 | + private static class ReactiveHealthIndicators { |
| 88 | + |
| 89 | + public Map<String, HealthIndicator> get(ApplicationContext applicationContext) { |
| 90 | + Map<String, HealthIndicator> indicators = new LinkedHashMap<>(); |
| 91 | + applicationContext.getBeansOfType(ReactiveHealthIndicator.class) |
| 92 | + .forEach((name, indicator) -> indicators.put(name, adapt(indicator))); |
| 93 | + return indicators; |
| 94 | + } |
| 95 | + |
| 96 | + private HealthIndicator adapt(ReactiveHealthIndicator indicator) { |
| 97 | + return () -> indicator.health().block(); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
64 | 102 | }
|
0 commit comments