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

com.ibm.cp4waiops.connectors.sdk.SDKCheck Maven / Gradle / Ivy

There is a newer version: 2.2.8
Show newest version
package com.ibm.cp4waiops.connectors.sdk;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;

/**
 * Base class for health checks that can be utilized by connectors
 */
public class SDKCheck implements HealthCheck {
    public enum Type {
        LIVENESS, READINESS,
    }

    private static ArrayList _instances = new ArrayList<>();

    /**
     * Adds a ConnectorManager instance to be monitored
     * 
     * @param manager
     */
    public static synchronized void addInstance(ConnectorManager manager) {
        if (manager != null) {
            _instances.add(manager);
        }
    }

    /**
     * Deprecated. Use addInstance() instead.
     * 
     * @param manager
     */
    @Deprecated
    public static synchronized void setInstance(ConnectorManager manager) {
        addInstance(manager);
    }

    /**
     * Removes the ConnectorManager instance from monitoring
     * 
     * @param manager
     */
    public static synchronized void removeInstance(ConnectorManager manager) {
        _instances.remove(manager);
    }

    private static synchronized List getInstances() {
        return _instances;
    }

    private String _name;
    private Type _type;

    public SDKCheck(String name, Type type) {
        _name = name;
        _type = type;
    }

    @Override
    public HealthCheckResponse call() {
        List instances = getInstances();
        // Verify there is at least one instance
        if (instances.size() == 0) {
            return HealthCheckResponse.builder().name(SDKCheck.class.getName()).down()
                    .withData("reason", "no connectors found").build();
        }
        // Check each instance
        SDKStatusBuilder builder = new SDKStatusBuilder();
        for (ConnectorManager instance : instances) {
            if (Objects.equals(_type, Type.LIVENESS)) {
                instance.checkIfLive(builder);
            } else {
                instance.checkIfReady(builder);
            }
        }
        return builder.toHealthCheckResponse(_name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy