spring-boot定时器

  • spring-boot框架中集成了一个轻量级的定时器框架,配置简单,容易上手。在spring-boot项目能够正常启动的前提下,保证pom文件中有如下配置:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  • 新建一个定时器类用于放置定时任务:
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@Async
public class jdMessageTisk {

    @Scheduled(cron = "0 0/1 * * * ?")
    public void pushDataScheduled1() {
        while (true) {
            System.out.println("定时任务1:"+new Date());
        }
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void pushDataScheduled2() {
        while (true) {
            System.out.println("定时执行2:" + new Date());
        }
    }
}
  • 定时器执行时间使用注解@Scheduled配置,应用cron规则。
  • 通过对定时参数设置,控制定时器的线程情况。新建参数类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class AsyncConfig {
    /**
     *corePoolSize:核心线程数
     * 核心线程会一直存活,及时没有任务需要执行
     * 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线程处理
     * 设置allowCoreThreadTimeout=true(默认false)时,核心线程会超时关闭
     *
     * maxPoolSize:最大线程数
     * 当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务
     * 当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常
     *
     * queueCapacity:任务队列容量(阻塞队列)
     * 当核心线程数达到最大时,新任务会放在队列中排队等待执行
     *
     */
    private int corePoolSize = 10;
    private int maxPoolSize = 200;
    private int queueCapacity = 10;
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}
  • 在项目启动的application类中加入@EnableScheduling注解,引导定时任务执行
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
//定时任务引导注解
@EnableScheduling
@SpringBootApplication
//@ImportResource(value="classpath:dubbo/dubbo-provider*.xml")
public class EmallserverInterfaceProviderApplication   {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(EmallserverInterfaceProviderApplication.class, args);
        Thread.sleep(100000000l);
    }
}
  • 运行结果:
    这里写图片描述

猜你喜欢

转载自blog.csdn.net/leo187/article/details/80959207