All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.management.health.indicator.AbstractHealthIndicator Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017-2020 original 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
 *
 * https://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 io.micronaut.management.health.indicator;

import io.micronaut.core.async.publisher.AsyncSingleResultPublisher;
import io.micronaut.health.HealthStatus;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.reactivestreams.Publisher;

import java.util.concurrent.ExecutorService;

/**
 * 

A base health indicator class to extend from that catches exceptions thrown from the * {@link #getHealthInformation()} method and updates the {@link HealthResult} with the exception information.

* * @param The health indication type * @author James Kleeh * @since 1.0 */ public abstract class AbstractHealthIndicator implements HealthIndicator { protected ExecutorService executorService; protected HealthStatus healthStatus; /** * @param executorService The executor service */ @Inject public void setExecutorService(@Named(TaskExecutors.BLOCKING) ExecutorService executorService) { this.executorService = executorService; } @Override public Publisher getResult() { if (executorService == null) { throw new IllegalStateException("I/O ExecutorService is null"); } return new AsyncSingleResultPublisher<>(executorService, this::getHealthResult); } /** * Provides information (typically a Map) to be returned. Set the {@link #healthStatus} field during execution, * otherwise {@link HealthStatus#UNKNOWN} will be used. * * @return Any details to be included in the response. */ protected abstract T getHealthInformation(); /** * Builds the whole health result. * * @return The health result to provide to the indicator. */ protected HealthResult getHealthResult() { HealthResult.Builder builder = HealthResult.builder(getName()); try { builder.details(getHealthInformation()); builder.status(this.healthStatus); } catch (Exception e) { builder.status(HealthStatus.DOWN); builder.exception(e); } return builder.build(); } /** * Used to populate the {@link HealthResult}. Provides a key to go along with the health information. * * @return The name of the indicator */ protected abstract String getName(); }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy