|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.health; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.Future; |
| 22 | + |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | + |
| 26 | +import org.springframework.core.task.AsyncTaskExecutor; |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link HealthIndicatorRunner} implementation that uses {@link AsyncTaskExecutor} to |
| 31 | + * invoke {@link HealthIndicator} instances. |
| 32 | + * |
| 33 | + * @author Vedran Pavic |
| 34 | + * @since 2.1.0 |
| 35 | + */ |
| 36 | +public class AsyncHealthIndicatorRunner implements HealthIndicatorRunner { |
| 37 | + |
| 38 | + private static final Log logger = LogFactory.getLog(AsyncHealthIndicatorRunner.class); |
| 39 | + |
| 40 | + private final AsyncTaskExecutor taskExecutor; |
| 41 | + |
| 42 | + /** |
| 43 | + * Create an {@link AsyncHealthIndicatorRunner} instance. |
| 44 | + * @param taskExecutor task executor used to run {@link HealthIndicator}s |
| 45 | + */ |
| 46 | + public AsyncHealthIndicatorRunner(AsyncTaskExecutor taskExecutor) { |
| 47 | + Assert.notNull(taskExecutor, "TaskExecutor must not be null"); |
| 48 | + this.taskExecutor = taskExecutor; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Map<String, Health> run(Map<String, HealthIndicator> healthIndicators) { |
| 53 | + Map<String, Health> healths = new HashMap<>(healthIndicators.size()); |
| 54 | + Map<String, Future<Health>> futures = new HashMap<>(); |
| 55 | + for (final Map.Entry<String, HealthIndicator> entry : healthIndicators |
| 56 | + .entrySet()) { |
| 57 | + Future<Health> future = this.taskExecutor |
| 58 | + .submit(() -> entry.getValue().health()); |
| 59 | + futures.put(entry.getKey(), future); |
| 60 | + } |
| 61 | + for (Map.Entry<String, Future<Health>> entry : futures.entrySet()) { |
| 62 | + try { |
| 63 | + healths.put(entry.getKey(), entry.getValue().get()); |
| 64 | + } |
| 65 | + catch (Exception e) { |
| 66 | + logger.warn("Error invoking health indicator '" + entry.getKey() + "'", |
| 67 | + e); |
| 68 | + healths.put(entry.getKey(), Health.down(e).build()); |
| 69 | + } |
| 70 | + } |
| 71 | + return healths; |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments