|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 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.Callable; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | +import java.util.concurrent.Executors; |
| 24 | +import java.util.concurrent.Future; |
| 25 | +import java.util.concurrent.ThreadFactory; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | + |
| 28 | +import org.apache.commons.logging.Log; |
| 29 | +import org.apache.commons.logging.LogFactory; |
| 30 | + |
| 31 | +import org.springframework.util.Assert; |
| 32 | +import org.springframework.util.ClassUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Default implementation of {@link HealthIndicatorRunner}. |
| 36 | + * |
| 37 | + * @author Vedran Pavic |
| 38 | + */ |
| 39 | +public class DefaultHealthIndicatorRunner implements HealthIndicatorRunner { |
| 40 | + |
| 41 | + private static final Log logger = LogFactory.getLog(DefaultHealthIndicatorRunner.class); |
| 42 | + |
| 43 | + private DefaultHealthIndicatorRunnerProperties properties; |
| 44 | + |
| 45 | + private ExecutorService executor; |
| 46 | + |
| 47 | + /** |
| 48 | + * Create a {@link DefaultHealthIndicatorRunner} instance. |
| 49 | + * @param properties the configuration properties |
| 50 | + */ |
| 51 | + public DefaultHealthIndicatorRunner(DefaultHealthIndicatorRunnerProperties properties) { |
| 52 | + Assert.notNull(properties, "Properties must not be null"); |
| 53 | + this.properties = properties; |
| 54 | + if (this.properties.isRunInParallel()) { |
| 55 | + this.executor = Executors.newFixedThreadPool( |
| 56 | + this.properties.getThreadCount(), new WorkerThreadFactory()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a {@link DefaultHealthIndicatorRunner} instance. |
| 62 | + */ |
| 63 | + public DefaultHealthIndicatorRunner() { |
| 64 | + this(new DefaultHealthIndicatorRunnerProperties()); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Map<String, Health> run(Map<String, HealthIndicator> healthIndicators) { |
| 69 | + Map<String, Health> healths = new HashMap<String, Health>(); |
| 70 | + if (this.properties.isRunInParallel()) { |
| 71 | + Assert.notNull(this.executor, "Executor must not be null"); |
| 72 | + Map<String, Future<Health>> futures = new HashMap<String, Future<Health>>(); |
| 73 | + for (final Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) { |
| 74 | + Future<Health> future = this.executor.submit(new Callable<Health>() { |
| 75 | + @Override |
| 76 | + public Health call() throws Exception { |
| 77 | + return entry.getValue().health(); |
| 78 | + } |
| 79 | + }); |
| 80 | + futures.put(entry.getKey(), future); |
| 81 | + } |
| 82 | + for (Map.Entry<String, Future<Health>> entry : futures.entrySet()) { |
| 83 | + try { |
| 84 | + healths.put(entry.getKey(), entry.getValue().get()); |
| 85 | + } |
| 86 | + catch (Exception e) { |
| 87 | + logger.warn("Error invoking health indicator '" + entry.getKey() + "'", e); |
| 88 | + healths.put(entry.getKey(), Health.down(e).build()); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + else { |
| 93 | + for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) { |
| 94 | + healths.put(entry.getKey(), entry.getValue().health()); |
| 95 | + } |
| 96 | + } |
| 97 | + return healths; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * {@link ThreadFactory} to create the worker threads. |
| 102 | + */ |
| 103 | + private static class WorkerThreadFactory implements ThreadFactory { |
| 104 | + |
| 105 | + private final AtomicInteger threadNumber = new AtomicInteger(1); |
| 106 | + |
| 107 | + @Override |
| 108 | + public Thread newThread(Runnable r) { |
| 109 | + Thread thread = new Thread(r); |
| 110 | + thread.setName(ClassUtils.getShortName(getClass()) + |
| 111 | + "-" + this.threadNumber.getAndIncrement()); |
| 112 | + return thread; |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments