springboot-定时任务

1.pom的引入

     定时任务不需要引入特别的依赖(也可以说在springboot中已经集成到spring-boot-starter里面了)

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
	</dependency>
</dependencies>

2.配置

 定时任务 只需要简单两步  

举例:

(1)启动类加上注解 @EnableScheduling  

@SpringBootApplication
@EnableScheduling
public class SpringbootSchedulingApplication {

    public static void main(String[] args) {      SpringApplication.run(SpringbootSchedulingApplication.class, args);
    }
}

(2)在需要执行的方法(定时任务实现类)上加上 @Scheduled(cron="*/9 * * * * ?")

@Component
public class SchedulerTask1 {

    @Scheduled(cron="*/9 * * * * ?")
    private  void  task(){
        System.out.println("定时任务");
    }
}

参数说明

@Scheduled 有两种参数设置方法,一种是 cron="*/9 * * * * ?",一种是 fixedRate = 9000,都表示每隔9秒执行一次。

fixedRate 说明

  • @Scheduled(fixedRate = 9000) :上一次开始执行时间点之后9秒再执行
  • @Scheduled(fixedDelay = 9000) :上一次执行完毕时间点之后9秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=9000) :第一次延迟1秒后执行,之后按fixedRate的规则每9秒执行一次


spring的定时任务默认是单线程多个任务执行起来时间会有问题(B任务会因为A任务执行起来需要20S而被延后20S执行)
因此可以添加线程池:
@Configuration
@EnableScheduling
public class SchedulingConfigurerConfiguration implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.setThreadNamePrefix("project-init-poolScheduler-");
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}



添加一个完整的例子:


配置:

package com.ciicgat.employeefinancial.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration
@EnableScheduling
public class SchedulingConfigurerConfiguration implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.setThreadNamePrefix("project-init-poolScheduler-");
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}

方法:

@Service
public class ScheduleFactory {
    @Autowired
    ScheduleService scheduleService;
    @Scheduled(fixedDelay = 60 * 1000 * 5)
    public void syncProductList() {
        scheduleService.syncProductList();
    }

    @Scheduled(fixedDelay = 60 * 1000 * 8)
    public  void  syncProductDetails(){
        scheduleService.syncProductDetails();
    }

}

注意:扫描注解的时候 ,需要扫描到这个service的包 (这个service的包在base-package下面)





  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值