简体   繁体   中英

Time Scheduling in Spring Boot

I have the List of the requested user and I want to upload the list of the user every day at 7 pm on each day. how can i do that using Spring Boot.And yes, it should also check whether the list is available or not.

You can achieve this with @Scheduled annotation in one of your methods of the bean. In order to enable scheduling you need to put @EnableScheduling annotation in one of your config classes, can be the main class:

@SpringBootApplication
@EnableScheduling
public class TestingApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestingApplication.class, args);
    }
}

Then you create a class, annotate it with @Component and create a method with @Scheduled annotation with a cron statement inside:

@Component
public class MyWorkerComponent {

    @Autowired
    private MyListChecker myListChecker;

    @Scheduled(cron = "0 0 19 * * ?")
    public void doTheListThingy() {
        if (myListChecker.isTheListAvailable()) {
            // your task logic
        }
    }
}

First,you should make a descition when your application have more than one instance the task need to executed once or can executed more than once.

If the task can executed more than once,the method provided by @Pijotrek and @mkjh is good. If the task must executed only once you must use Quartz Scheduler or other framework support distribute scheduling task sysytem.more information you can get from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM