[英]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. 我有被请求用户的列表,我想每天每天晚上7点上传用户列表。 how can i do that using Spring Boot.And yes, it should also check whether the list is available or not. 我如何使用Spring Boot做到这一点,是的,它还应该检查列表是否可用。
You can achieve this with @Scheduled
annotation in one of your methods of the bean. 您可以在bean的一种方法中使用@Scheduled
批注实现此目的。 In order to enable scheduling you need to put @EnableScheduling
annotation in one of your config classes, can be the main class: 为了启用调度,您需要将@EnableScheduling
注释放在您的一个配置类中,可以是主类:
@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
对其进行注释,并使用内部带有cron语句的@Scheduled
注释创建一个方法:
@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. 如果任务可以执行多次,则@Pijotrek和@mkjh提供的方法很好。 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 如果任务必须只执行一次,你必须使用Quartz Scheduler
或其他框架的支持分发调度任务sysytem.more你可以从信息在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.