我有一个TimerTask,它应该基于Timer.schedule运行。
问题是当应用程序启动时它只运行一次...
也许有什么事情还未完成,但我无法理解是什么...
这是我的扩展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
方法来实现重复执行。 - VGR