简体   繁体   English

Spring Boot中的时间安排

[英]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.

相关问题 调度:在spring boot中只执行一次任务 - Scheduling: execute tasks only one time in spring boot Spring 启动Web服务/微服务和调度 - Spring boot Webservice / Microservices and scheduling 是否可以在spring boot/java中实现每个用户在不同时间动态调度一个任务 - is it possible to achieve dynamic scheduling of a task for each user at different time in spring boot/java 使用石英调度程序在弹簧启动中进行动态作业调度 - Dynamic job scheduling in spring boot with quartz scheduler Spring Boot调度不同任务的间隔时间不同 - Spring boot scheduling of different tasks at different intervals 调度不适用于 spring 启动微服务中的 rest 调用 - Scheduling not working for rest calls in spring boot microservice Spring Boot中分布式任务调度和作业队列的建议 - Suggestions for distributed task scheduling and Job queue in spring boot spring boot scheduling在方法级别启用/禁用任务 - spring boot scheduling enable/disable tasks in method level 在 spring boot 应用程序中安排多个批处理作业 - scheduling more than one batch job in spring boot application Spring Boot Cron表达式调度,调用ApplicationListener的onApplicationEvent - Spring boot cron expression scheduling, calling onApplicationEvent of ApplicationListener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM