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

com.pamirs.pradar.upload.HttpScheduledUploader Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.pamirs.pradar.upload;

import com.pamirs.pradar.common.HttpUtils;
import com.shulie.instrument.simulator.api.executors.ExecutorServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * 周期性轮询上报
 *
 * @author shiyajian
 * create: 2020-07-20
 */
public abstract class HttpScheduledUploader extends HttpUploader {

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

    /**
     * 已经上传的次数
     */
    protected long uploadCount;

    /**
     * 是否在运行中
     */
    private boolean running;

    /**
     * 时间周期,每过多少时间上报一次,单位:秒
     */
    protected int uploadPeriodSecond;

    protected ScheduledFuture future;

    public HttpScheduledUploader(String name, String postUrl, int uploadPeriodSecond) {
        super(name, postUrl);
        this.uploadCount = 0;
        this.running = false;
        this.uploadPeriodSecond = (uploadPeriodSecond <= 0) ? 3 : uploadPeriodSecond; // default 3
    }


    /**
     * 是否准备好了
     * 准备好了开始上报,否则不会进行上报
     */
    protected abstract boolean isReady();

    /**
     * 已经上传的次数
     */
    public long getUploadedCount() {
        return this.uploadCount;
    }

    @Override
    public synchronized void start() {
        if (!enabled()) {
            return;
        }
        if (running) {
            return;
        }
        running = true;

        future = ExecutorServiceFactory.getFactory().scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (!running) {
                    return;
                }
                if (!isReady()) {
                    return;
                }
                HttpUtils.HttpResult httpResult = innerUpload();
                if (!httpResult.isSuccess()) {
                    LOGGER.error("Agent Health Check failed,access Config Center failed! HttpStatus Code: " + httpResult.getStatus() + ",message :" + httpResult.getResult());
                } else {
                    LOGGER.info("Agent Health Check successful!");
                }
                uploadCount++;
            }
        }, uploadPeriodSecond, uploadPeriodSecond, TimeUnit.SECONDS);
    }

    public synchronized void stop() {
        if (!running) {
            return;
        }
        running = false;
        if (future != null && !future.isCancelled() && !future.isDone()) {
            future.cancel(true);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy