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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章