博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot-定时任务
阅读量:4160 次
发布时间:2019-05-26

本文共 2999 字,大约阅读时间需要 9 分钟。

1.pom的引入

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

org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
true

2.配置

 定时任务 只需要简单两步  

举例:

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

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

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

@Componentpublic 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@EnableSchedulingpublic 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@EnableSchedulingpublic 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);    }}

方法:

@Servicepublic 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下面)

转载地址:http://rfjxi.baihongyu.com/

你可能感兴趣的文章
mapnik的demo运行
查看>>
python支持下的mapnik安装
查看>>
milvus手册
查看>>
多目标跟踪的简单理解
查看>>
Near-Online Multi-target Tracking with Aggregated Local Flow Descriptor
查看>>
Joint Tracking and Segmentation of Multiple Targets
查看>>
Subgraph Decomposition for Multi-Target Tracking
查看>>
JOTS: Joint Online Tracking and Segmentation
查看>>
CDT: Cooperative Detection and Tracking for Tracing Multiple Objects in Video Sequences
查看>>
Improving Multi-frame Data Association with Sparse Representations for Robust Near-online Multi-ob
查看>>
Virtual Worlds as Proxy for Multi-Object Tracking Analysis
查看>>
Multi-view People Tracking via Hierarchical Trajectory Composition
查看>>
Online Multi-Object Tracking via Structural Constraint Event Aggregation
查看>>
The Solution Path Algotithm for Identity-Aware Multi-Object Tracking
查看>>
Groupwise Tracking of Crowded Similar-Appearance Targets from Low-Continuity Image Sequences
查看>>
CDTS: Collaborative Detection, Tracking, and Segmentation for Online Multiple Object Segmentation
查看>>
Deep Network Flow for Multi-Object Tracking
查看>>
Multiple People Tracking by Lifted Multicut and Person Re-identification
查看>>
Multi-Object Tracking with Quadruplet Convolutional Neural Networks
查看>>
关于多目标跟踪的一点理解
查看>>