package com.zskk.dicom.monitor.runner; import com.zskk.dicom.monitor.config.Configs; import java.util.concurrent.TimeUnit; public abstract class BaseRemoveRunner extends AbstractRemove implements Runnable{ @Override public void run() { while (true) { try { while (empty()) { printEmptyAndSleep(); if(exit()) { return; } } if(remove()) { printSuccessAndSleep(); } else { printFailAndSleep(); } } catch (InterruptedException e) { e.printStackTrace(); } } } private int MIN_FAIL_SLEEP_TIME = 1; private int MAX_FAIL_SLEEP_TIME = 5; private int failSleepTime = 0; private int MIN_EMPTY_SLEEP_TIME = 10; private int MAX_EMPTY_SLEEP_TIME = 60; private int emptySleepTime = 0; protected abstract boolean exit(); protected void printFailAndSleep() throws InterruptedException { Configs.sysLog.warn(getRunnerName() + ": " + action() + " fail"); if(failSleepTime < MAX_FAIL_SLEEP_TIME) { failSleepTime += MIN_FAIL_SLEEP_TIME; } TimeUnit.SECONDS.sleep(failSleepTime); } protected abstract String getRunnerName(); private String action() { return "upload"; } protected void printSuccessAndSleep() { Configs.sysLog.info(getRunnerName() + ": " + action() + " success!" + getQueueName() + " size :" + size()); failSleepTime = 0; emptySleepTime = 0; } protected void printEmptyAndSleep() throws InterruptedException { Configs.sysLog.warn(getRunnerName() + ": " + getQueueName() + " is empty"); if(emptySleepTime < MAX_EMPTY_SLEEP_TIME) { emptySleepTime += MIN_EMPTY_SLEEP_TIME; } TimeUnit.SECONDS.sleep(emptySleepTime); } protected abstract boolean empty(); protected abstract int size(); protected abstract String getQueueName(); }