gpt4 book ai didi

Java Timer.schedule 只运行一次

转载 作者:行者123 更新时间:2023-12-05 00:17:39 25 4
gpt4 key购买 nike

我有一个基于 Timer.schedule 运行的 TiimerTask。
问题是它只在应用程序启动时运行一次...
也许这是未决的事情,但我无法理解......

这是我的类(class),它扩展了 TimerTask

public class ClientScheduler extends TimerTask {

public String serverUrl = Start.getHost();
public String append = "/client/checkVersion";
public String numeroClient = null;
public String actualVersion = null;
public String filePrefix = "eparkclient-";
public String fileSuffix = "-jar-with-dependencies.jar";
private final Logger logger = Logger.getLogger(ClientScheduler.class);

public ClientScheduler() {

}

@Override
public void run() {



logger.debug("Scheduler starts");
String finalUrl = null;
try {
numeroClient = PropertyConfig.getClientId();

actualVersion = PropertyConfig.getFirmwareVersion();
finalUrl = serverUrl + append + "?numeroClient=" + numeroClient;

HttpConnection http = new HttpConnection();
String result = http.getHTTPResponse(finalUrl);
JSONObject json = new JSONObject(result);
String firmwareVersion = json.getString("firmwareVersion");
Boolean updated = json.getBoolean("firmwareUpdated");

if(!actualVersion.equalsIgnoreCase(firmwareVersion)){
//scarico il file dall'ftp
FTPDownload ftp = new FTPDownload();
String filename = filePrefix+firmwareVersion+fileSuffix;
logger.debug("filename è "+filename);
boolean success = ftp.getFile(filename);
if(success) {
//scrivo la versione nuova sul file
PropertyConfig.setFirmwareVersion(firmwareVersion);
//comunico al server l'aggiornamento riuscito
HttpConnection answer = new HttpConnection();
String url = serverUrl + "/client/pushUpdate?numeroClient=" + numeroClient + "&firmwareVersion=" +
firmwareVersion + "&updated=0";

String r = answer.getHTTPResponse(url);
System.exit(0);

}


} else if(actualVersion.equalsIgnoreCase(firmwareVersion) && !updated){ //ho riavviato, devo aggiornare il DB

HttpConnection answer = new HttpConnection();
String url = serverUrl + "/client/pushUpdate?numeroClient=" + numeroClient + "&firmwareVersion=" +
firmwareVersion + "&updated="+!updated;

String r = answer.getHTTPResponse(url);

} else {
logger.debug("Non dobbiamo fare niente");
}
} catch (IOException e) {
logger.error("Errore Property", e);

}
}

}

应用程序以这种方式启动时调用任务
public static void main(String[] args) throws Exception {
logger.info("L'ip del client è " + getCurrentIP());

//faccio partire lo scheduler
logger.debug("Scheduler called");

Timer timer = new Timer();
timer.schedule(new ClientScheduler(), 10*1000);

最佳答案

您只安排计时器任务一次。

schedule 方法定义为

schedule(TimerTask task, long delay)

Schedules the specified task for execution after the specified delay.



但是你需要使用这个方法:

schedule(TimerTask task, long delay, long period)

Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.



在此处查看 Javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html#schedule-java.util.TimerTask-long-long-

关于Java Timer.schedule 只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39603970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com