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

com.cucumber.utils.engineering.poller.MethodPoller Maven / Gradle / Ivy

package com.cucumber.utils.engineering.poller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.Duration;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class MethodPoller {
    private Logger log = LogManager.getLogger();

    private Duration pollDurationSec = Duration.ofSeconds(30);
    private Long pollIntervalMillis = 3000L;
    private Double exponentialBackOff = 1.0;

    private Supplier pollMethod = null;
    private Predicate pollResultPredicate = null;

    public MethodPoller duration(Long pollIntervalMillis) {
        return duration((Duration) null, pollIntervalMillis);
    }

    public MethodPoller duration(Integer pollDurationSec) {
        return duration(pollDurationSec, null);
    }

    public MethodPoller duration(Integer pollDurationSec, Long pollIntervalMillis) {
        return duration(pollDurationSec != null ? Duration.ofSeconds(pollDurationSec) : null, pollIntervalMillis);
    }

    public MethodPoller duration(Duration pollDurationSec, Long pollIntervalMillis) {
        this.pollDurationSec = pollDurationSec != null ? pollDurationSec : this.pollDurationSec;
        this.pollIntervalMillis = pollIntervalMillis != null ? pollIntervalMillis : this.pollIntervalMillis;
        return this;
    }

    public MethodPoller exponentialBackOff(Double exp) {
        if (exp != null) {
            this.exponentialBackOff = exp;
        }
        return this;
    }

    public MethodPoller method(Supplier supplier) {
        pollMethod = supplier;
        return this;
    }

    public MethodPoller until(Predicate predicate) {
        pollResultPredicate = predicate;
        return this;
    }

    public T poll() {
        log.debug("Polling for result...");
        boolean pollSucceeded = false;
        boolean pollTimeout = false;
        T result = null;
        while (!pollSucceeded && !pollTimeout) {
            long start = System.currentTimeMillis();
            result = pollMethod.get();
            pollSucceeded = pollResultPredicate.test(result);
            if (!pollSucceeded) {
                try {
                    log.debug("Poll failed, I'll take another shot after {}ms", pollIntervalMillis);
                    Thread.sleep(pollIntervalMillis);
                    pollIntervalMillis = (long) (pollIntervalMillis * exponentialBackOff);
                    long elapsed = System.currentTimeMillis() - start;
                    pollDurationSec = pollDurationSec.minusMillis(elapsed);
                    if (pollDurationSec.isZero() || pollDurationSec.isNegative()) {
                        pollTimeout = true;
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        log.debug(!pollTimeout ? "Found correct result" : "Poll timeout");
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy