springboot 定时任务开发配置

相关链接:https://blog.csdn.net/xubenxismile/article/details/96453704

一、添加依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.4.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
</properties>

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

二、启动类****Application.java配置 @EnableScheduling

package com.vol;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
//定时任务
@EnableScheduling
public class SpringbootBaseApplication {

	public static void main(String[] args) {

	    SpringApplication.run(SpringbootBaseApplication1.class, args);

	}

}

三、编写定时任务,建议定时任务单独建一个目录

package com.vol.timingTasks;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Author 徐本锡
 * @Date 2019/7/18
 **/

@Component
public class exampleTask {
    @Scheduled(cron="0  */1 7-22 * * ?")//每过1分钟(7---22点) 执行一次
//    @Scheduled(fixedRate = 1000*30)//上次执行完毕之后30秒
    public void runTask(){
        taskA();
        taskB();

    }

    /**
     * @Author 徐本锡
     * @Date 2019/7/18
     **/
    public void taskA(){
        for(int i=0; i<10; i++){
            System.out.println("taskA----->"+String.valueOf(i+1));
        }
    }


    /**
     * @Author 徐本锡
     * @Date 2019/7/18
     **/
    public void taskB(){
        for(int i=0; i<10; i++){
            System.out.println("taskB----->"+String.valueOf(i+1));
        }
    }
}


四、配置定时任务多线程

package com.vol.config;

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

import java.util.concurrent.Executors;

/**
  * @Author 徐本锡
  * @Description //定时任务配置类   多线程
  * @Date 2019/7/18
  * @Param
  * @return
  **/

@Configuration
@EnableScheduling
public class TimingTasksConfig implements SchedulingConfigurer {

    //所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //springboot定时任务默认是单线程,现在将线程池改为10个线程的线程池
        //设定一个长度10的定时任务线程池
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }

//    springboot的定时任务默认是单线程的,源码如下:
//    if (this.taskScheduler == null) {
//        this.localExecutor = Executors.newSingleThreadScheduledExecutor();
//        this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
//    }
//      定时任务的线程池大小只有1,即单线程线程池
//      public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
//        return new Executors.DelegatedScheduledExecutorService
//                (new ScheduledThreadPoolExecutor(1));
//      }

}

猜你喜欢

转载自blog.csdn.net/xubenxismile/article/details/96480780