zoukankan      html  css  js  c++  java
  • 【Spring Boot】定时任务

    【Spring Boot】定时任务

    测试用业务Service

    package com.example.schedule.service;
    import org.springframework.stereotype.Service;
    @Service
    public class UserService {
        public void syncUserData(){
            System.out.println("syncUserData");
        }
    }
    
    package com.example.schedule.service;
    import org.springframework.stereotype.Service;
    @Service
    public class StudentService {
        public void syncStudentData(){
            System.out.println("syncStudentData");
        }
    }

    1、使用Spring的@Scheduled注解

    使用@EnableScheduling注解启用Spring定时任务

    package com.example.schedule;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    @SpringBootApplication
    @EnableScheduling
    public class ScheduleApplication {
        public static void main(String[] args) {
            SpringApplication.run(ScheduleApplication.class, args);
        }
    }

    定时方法

    package com.example.schedule.scheduled_bean;
    import com.example.schedule.service.StudentService;
    import com.example.schedule.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Component
    public class YcxScheduledBean {
        @Autowired
        UserService userService;
        @Autowired
        StudentService studentService;
        @Scheduled(cron="*/2 * * * * ?")
        public void syncData() {
            System.out.println(">>> YcxScheduledBean");
            userService.syncUserData();
            studentService.syncStudentData();
        }
    }

    2、使用quartz

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>

    继承QuartzJobBean

    package com.example.schedule.job;
    
    import com.example.schedule.service.StudentService;
    import com.example.schedule.service.UserService;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    
    public class YcxSyncJob extends QuartzJobBean {
        @Autowired
        UserService userService;
    
        @Autowired
        StudentService studentService;
        @Override
        protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
            System.out.println(">>> QuartzJobBean 同步任务");
            userService.syncUserData();
            studentService.syncStudentData();
        }
    }
  • 相关阅读:
    tips
    【十大算法实现之KNN】KNN算法实例(含测试数据和源码)
    智力趣题几则
    JAVA知多少
    R语言(入门小练习篇)
    文本分类,数据挖掘和机器学习
    推荐系统的循序进阶读物(从入门到精通)
    【贪心】PAT 1033. To Fill or Not to Fill (25)
    博弈故事一则——海盗分金币问题
    基于WordNet的英文同义词、近义词相似度评估及代码实现
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/10998266.html
Copyright © 2011-2022 走看看