Java Timer.schedule runs only once

besmart picture besmart · Sep 20, 2016 · Viewed 8.7k times · Source

I have a TiimerTask which should run based on a Timer.schedule. The problem is it only runs once when application starts... Maybe it's something pending, but I cannot understand what...

this is my class which extends 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);

                }
            }

    }

The task is called when the application starts in this way

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);

Answer

thst picture thst · Sep 20, 2016

You schedule the timer task only once.

The schedule method is defined as

schedule(TimerTask task, long delay)

Schedules the specified task for execution after the specified delay.

But you need to use this method:

schedule(TimerTask task, long delay, long period)

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

See Javadocs here: https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html#schedule-java.util.TimerTask-long-long-