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

io.takari.orchestra.plugins.nexus.HttpTask Maven / Gradle / Ivy

package io.takari.orchestra.plugins.nexus;

import io.takari.bpm.api.BpmnError;
import io.takari.orchestra.common.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Named;
import java.net.HttpURLConnection;
import java.net.URL;

// TODO move into a separate module
@Named
public class HttpTask implements Task {

    private static final Logger log = LoggerFactory.getLogger(HttpTask.class);
    private static final long DEFAULT_AVAILABILITY_CHECK_DELAY = 3000;

    @Override
    public String getKey() {
        return "http";
    }

    private boolean isAvailable(String url) {
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            int code = conn.getResponseCode();
            if (code >= 400) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void waitFor(String url, long timeout) {
        if (url == null || url.trim().isEmpty()) {
            return;
        }

        long t1 = System.currentTimeMillis();
        while (isAvailable(url)) {
            long t2 = System.currentTimeMillis();
            if (t2 - t1 >= timeout) {
                throw new BpmnError("Timeout waiting for url");
            }

            log.info("waitFor ['{}', {}] -> waiting...", url, timeout);

            try {
                Thread.sleep(DEFAULT_AVAILABILITY_CHECK_DELAY);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        log.info("waitFor ['{}', {}] -> done", url, timeout);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy