001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.health;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Map;
022import java.util.Optional;
023import java.util.function.BiConsumer;
024
025import org.apache.camel.CamelContextAware;
026import org.apache.camel.Service;
027
028/**
029 * An health check service that invokes the checks registered on the {@link HealthCheckRegistry}
030 * according to a schedule.
031 */
032public interface HealthCheckService extends Service, CamelContextAware {
033    /**
034     * Add a listener to invoke when the state of a check change.
035     *
036     * @param consumer the event listener.
037     */
038    void addStateChangeListener(BiConsumer<HealthCheck.State, HealthCheck> consumer);
039
040    /**
041     * Remove the state change listener.
042     *
043     * @param consumer the event listener to remove.
044     */
045    void removeStateChangeListener(BiConsumer<HealthCheck.State, HealthCheck> consumer);
046
047    /**
048     * Sets the options to be used when invoking the check identified by the
049     * given id.
050     *
051     * @param id the health check id.
052     * @param options the health check options.
053     */
054    void setHealthCheckOptions(String id, Map<String, Object> options);
055
056    /**
057     * @see {@link #call(String, Map)}
058     *
059     * @param id the health check id.
060     * @return the result of the check or {@link Optional#empty()} if the id is unknown.
061     */
062    default Optional<HealthCheck.Result> call(String id) {
063        return call(id, Collections.emptyMap());
064    }
065
066    /**
067     * Invokes the check identified by the given <code>id</code> with the given
068     * <code>options</code>.
069     *
070     * @param id the health check id.
071     * @param options the health check options.
072     * @return the result of the check or {@link Optional#empty()} if the id is unknown.
073     */
074    Optional<HealthCheck.Result> call(String id, Map<String, Object> options);
075
076    /**
077     * Notify the service that a check has changed status. This may be useful for
078     * stateful checks like checks rely on tcp/ip connections.
079     *
080     * @param check the health check.
081     * @param result the health check result.
082     */
083    void notify(HealthCheck check, HealthCheck.Result result);
084
085    /**
086     * Return a list of the known checks status.
087     *
088     * @return the list of results.
089     */
090    Collection<HealthCheck.Result> getResults();
091
092    /**
093     * Access the underlying concrete HealthCheckService implementation to
094     * provide access to further features.
095     *
096     * @param clazz the proprietary class or interface of the underlying concrete HealthCheckService.
097     * @return an instance of the underlying concrete HealthCheckService as the required type.
098     */
099    default <T extends HealthCheckService> T unwrap(Class<T> clazz) {
100        if (HealthCheckService.class.isAssignableFrom(clazz)) {
101            return clazz.cast(this);
102        }
103
104        throw new IllegalArgumentException(
105            "Unable to unwrap this HealthCheckService type (" + getClass() + ") to the required type (" + clazz + ")"
106        );
107    }
108}