Spring Boot计划任务实现

1简介

前一个博客,自己参考《Java EE开发的颠覆者 Spring Boot实战》中的内容梳理了Spring Boot对多线程的支持。Spring 对于计划任务支持的也很简单。与异步任务支持的模式一直,首先使用注解@EnableScheduling开启对计划任务的支持,然后在要执行计划任务的方法上注释@Scheduled,声明这是一个计划任务即可。

2代码演示

2.1pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cetc52</groupId>
    <artifactId>testThread</artifactId>
    <version>1.0-SNAPSHOT</version>

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

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

2.2配置类开启对计划任务的支持

package com.cetc52.task.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 配置类,开启定时任务的支持
 *
 * @Owner:
 * @Time: 2019/3/25-22:24
 */
@Configuration
@EnableScheduling
@ComponentScan("com.cetc52.task.service")
public class TaskExecutorConfig {
}

注意:ComponentScan为待扫的包,里面要属于包路径。

2.3计划任务执行类@Scheduled

package com.cetc52.task.service;

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

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 计划任务执行类
 *
 * @Owner:
 * @Time: 2019/3/25-22:25
 */
@Service
public class ScheduledTaskService {
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        System.out.println("每隔5秒执行一次: "+SIMPLE_DATE_FORMAT.format(new Date()));
    }

    @Scheduled(cron = "0 28 11 ? * *")
    public void fixTimeExecution() {
        System.out.println("在指定时间 "+SIMPLE_DATE_FORMAT.format(new Date())+ "执行");
    }
}

2.4主类调用演示

package com.cetc52.task;

import com.cetc52.task.config.TaskExecutorConfig;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 主类
 *
 * @Owner:
 * @Time: 2019/3/25-22:30
 */
@SpringBootApplication
public class TaskApplication {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext configApplicationContext =
                new AnnotationConfigApplicationContext(TaskExecutorConfig.class);

    }
}

2.5演示结果

每隔5秒执行一次: 22:32:13
每隔5秒执行一次: 22:32:18
每隔5秒执行一次: 22:32:23
每隔5秒执行一次: 22:32:28
每隔5秒执行一次: 22:32:33
每隔5秒执行一次: 22:32:38
每隔5秒执行一次: 22:32:43
每隔5秒执行一次: 22:32:48
每隔5秒执行一次: 22:32:53
每隔5秒执行一次: 22:32:58
每隔5秒执行一次: 22:33:03
每隔5秒执行一次: 22:33:08
每隔5秒执行一次: 22:33:13
每隔5秒执行一次: 22:33:18
每隔5秒执行一次: 22:33:23
每隔5秒执行一次: 22:33:28
每隔5秒执行一次: 22:33:33
每隔5秒执行一次: 22:33:38

符合预期,而自己在出差时也正是使用了这种模式来处理了访客登记、签离和超期未签离的告警的开发的,虽然自己觉得一点也不优雅,是很糟糕的实现。

2.6Could not find default ScheduledExecutorService bean

在执行该程序时,弹出了如下的错误

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.scheduling.TaskScheduler' available

查阅网页,发现这是任务框架的机制。详情参见Spring定时任务 Could not find default TaskScheduler bean异常处理

3下载

Spring Boot实战源码和pdf


版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lk142500/article/details/88808542