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

io.dropwizard.health.core.HealthCheckScheduler Maven / Gradle / Ivy

Go to download

Provides a health check implementation that performs ongoing monitoring of an application's dependencies and includes an endpoint that can be called by a load balancer to determine if the application is healthy and thus able to retrieve traffic.

The newest version!
package io.dropwizard.health.core;

import io.dropwizard.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class HealthCheckScheduler {
    private static final Logger log = LoggerFactory.getLogger(HealthCheckScheduler.class);

    private final ScheduledExecutorService executorService;
    private final Map futures = new ConcurrentHashMap<>();

    public HealthCheckScheduler(final ScheduledExecutorService executorService) {
        this.executorService = executorService;
    }

    void scheduleInitial(final ScheduledHealthCheck check) {
        final Duration interval;
        if (check.isHealthy()) {
            interval = check.getSchedule().getCheckInterval();
        } else {
            interval = check.getSchedule().getDowntimeInterval();
        }

        schedule(check, check.getSchedule().getInitialDelay(), interval);
    }

    public void schedule(final ScheduledHealthCheck check, final boolean healthy) {
        unschedule(check.getName());

        final Duration interval;
        if (healthy) {
            interval = check.getSchedule().getCheckInterval();
        } else {
            interval = check.getSchedule().getDowntimeInterval();
        }

        schedule(check, interval, interval);
    }

    private void schedule(final ScheduledHealthCheck check, final Duration initialDelay, final Duration delay) {
        final ScheduledFuture taskFuture = executorService.scheduleWithFixedDelay(check, initialDelay.toMilliseconds(),
                delay.toMilliseconds(), TimeUnit.MILLISECONDS);
        futures.put(check.getName(), taskFuture);
        log.debug("Scheduled check: check={}", check);
    }

    public void unschedule(final String name) {
        final ScheduledFuture taskFuture = futures.get(name);
        if (taskFuture != null) {
            taskFuture.cancel(true);
            futures.remove(name);
            log.debug("Unscheduled check: name={}", name);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy