<commit_before>package org.axonframework.axonserver.connector.heartbeat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
 * Responsible to schedule generic tasks at a fixed rate.
 * The scheduling can be started an stopped at any time.
 *
 * @author <NAME>
 * @since 4.2.1
 */
public class Scheduler {

    private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class);

    private static final long DEFAULT_INITIAL_DELAY = 10_000;

    private static final long DEFAULT_RATE = 1_000;

    private final ScheduledExecutorService scheduler;

    private final long initialDelay;

    private final long rate;

    private final AtomicReference<ScheduledFuture> scheduledTask = new AtomicReference<>();

    private final Runnable runnable;

    /**
     * Constructs an instance of a {@link Scheduler} using a default initial delay of 10 seconds,
     * a default scheduling period of 1 second and a single thread {@link ScheduledExecutorService}.
     *
     * @param runnable the task to be scheduled
     */
    public Scheduler(Runnable runnable) {
        this(runnable, Executors.newSingleThreadScheduledExecutor());
    }

    /**
     * Constructs an instance of a {@link Scheduler} using a default initial delay of 10 seconds
     * and a default scheduling period of 1 second.
     * @param runnable the task to be scheduled
     * @param scheduler the {@link ScheduledExecutorService} to use for scheduling the task
     */
    public Scheduler(Runnable runnable, ScheduledExecutorService scheduler) {
        this(runnable, scheduler, DEFAULT_INITIAL_DELAY, DEFAULT_RATE);
    }

    /**
     * Primary constructor for {@link Scheduler}
     * @param runnable the task to be scheduled
     * @param scheduler the {@link ScheduledExecutorService} to use for scheduling the task
     * @param initialDelay the initial delay, in milliseconds
     * @param rate the scheduling period, in milliseconds
     */
    public Scheduler(Runnable runnable,
                     ScheduledExecutorService scheduler,
                     long initialDelay,
                     long rate) {
        this.runnable = runnable;
        this.scheduler = scheduler;
        this.initialDelay = initialDelay;
        this.rate = rate;
    }

    /**
     * Schedule the specified task accordingly with the specified scheduling setting.
     */
    public void start() {
        ScheduledFuture<?> task = scheduler.scheduleAtFixedRate(runnable,
                                                                initialDelay,
                                                                rate,
                                                                MILLISECONDS);
        if (!scheduledTask.compareAndSet(null, task)) {
            task.cancel(true);
            LOGGER.warn("{} already scheduled.", runnable.getClass().getSimpleName());
        }
    }

    /**
     * Interrupt the scheduled task if running and stop all the following scheduled executions.
     */
    public void stop() {
        ScheduledFuture<?> task = scheduledTask.get();
        if (task != null && scheduledTask.compareAndSet(task, null)) {
            task.cancel(true);
        } else {
            LOGGER.warn("{} already stopped.", runnable.getClass().getSimpleName());
        }
    }
}<commit_msg>Implemented high level heartbeat in order to detect disconnection from server as soon as possible. Improved readibility renaming runnable in task.<commit_after>package org.axonframework.axonserver.connector.heartbeat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
 * Responsible to schedule generic tasks at a fixed rate.
 * The scheduling can be started an stopped at any time.
 *
 * @author <NAME>
 * @since 4.2.1
 */
public class Scheduler {

    private static final Logger LOGGER = LoggerFactory.getLogger(Scheduler.class);

    private static final long DEFAULT_INITIAL_DELAY = 10_000;

    private static final long DEFAULT_RATE = 1_000;

    private final ScheduledExecutorService scheduler;

    private final long initialDelay;

    private final long rate;

    private final AtomicReference<ScheduledFuture> scheduledTask = new AtomicReference<>();

    private final Runnable task;

    /**
     * Constructs an instance of a {@link Scheduler} using a default initial delay of 10 seconds,
     * a default scheduling period of 1 second and a single thread {@link ScheduledExecutorService}.
     *
     * @param task the task to be scheduled
     */
    public Scheduler(Runnable task) {
        this(task, Executors.newSingleThreadScheduledExecutor());
    }

    /**
     * Constructs an instance of a {@link Scheduler} using a default initial delay of 10 seconds
     * and a default scheduling period of 1 second.
     * @param task the task to be scheduled
     * @param scheduler the {@link ScheduledExecutorService} to use for scheduling the task
     */
    public Scheduler(Runnable task, ScheduledExecutorService scheduler) {
        this(task, scheduler, DEFAULT_INITIAL_DELAY, DEFAULT_RATE);
    }

    /**
     * Primary constructor for {@link Scheduler}
     * @param task the task to be scheduled
     * @param scheduler the {@link ScheduledExecutorService} to use for scheduling the task
     * @param initialDelay the initial delay, in milliseconds
     * @param rate the scheduling period, in milliseconds
     */
    public Scheduler(Runnable task,
                     ScheduledExecutorService scheduler,
                     long initialDelay,
                     long rate) {
        this.task = task;
        this.scheduler = scheduler;
        this.initialDelay = initialDelay;
        this.rate = rate;
    }

    /**
     * Schedule the specified task accordingly with the specified scheduling setting.
     */
    public void start() {
        ScheduledFuture<?> scheduledFuture = scheduler.scheduleAtFixedRate(task,
                                                                           initialDelay,
                                                                           rate,
                                                                           MILLISECONDS);
        if (!scheduledTask.compareAndSet(null, scheduledFuture)) {
            scheduledFuture.cancel(true);
            LOGGER.warn("{} already scheduled.", task.getClass().getSimpleName());
        }
    }

    /**
     * Interrupt the scheduled task if running and stop all the following scheduled executions.
     */
    public void stop() {
        ScheduledFuture<?> scheduledFuture = scheduledTask.get();
        if (scheduledFuture != null && scheduledTask.compareAndSet(scheduledFuture, null)) {
            scheduledFuture.cancel(true);
        } else {
            LOGGER.warn("{} already stopped.", task.getClass().getSimpleName());
        }
    }
}
{'fraction_non_alphanumeric': 0.07720739219712526, 'fraction_numerical': 0.0054757015742642025, 'mean_word_length': 2.4429783223374177, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5049099', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1762, 'n_words': 588}