How to run a specific method at a specific date and time?

I need to get my Java application to do a couple of things at a specific date and time. The scenario is as follows:

1- user set a specific time and frequency (every day, every month)
2- system starts a trigger for the request

3- once that pre-defined frequency and time are reached 
 3.1 - system performs the required actions that are kept in a method

I found this answer , but could not get it to work.

An example will be appreciated.

0
source share
3 answers

There are several java scheduler infrastructures for performing tasks at a specified time, interval, frequency. Apache quartz is one of the most used.

or just use java ScheduledExecutorService

+3
source

Executor jdk, :

 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 ScheduledFuture<?> handle scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() { System.out.println("your code is here :)"); }
        }, 1, 100, TimeUnit.MINUT);

1 100 .

, handle.cancel(true)

+2

Source: https://habr.com/ru/post/1677404/


All Articles