Skip to content
Advertisement

i want to run my code block again by the minute

I want to run my code block again every minute. Because I need to pull data from a site continuously gradually. Even if the site crashes or the machine crashes, I want it to run the data withdrawal codes again.

`

ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

Runnable task = () -> {

///CODE///

catch (InterruptedException e) {
    throw new RuntimeException(e);
} finally {

    driver.close();

}

executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES);



`

I have tried this system, but when the program or site crashes. It just keeps the program open and it does not run the codes that it needs to run again every 1 minute. This cycle works when the site and the program do not crash.

Advertisement

Answer

If you throw an exception (or dont handle thrown exceptions) in your run() method the scheduler will stop processing the task.

So you have to handle all exceptions try {...} catch(Exception e) {} (at least). See: ScheduledExecutorService Exception handling

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement