SpringBoot @Scheduled 定时任务 入门demo

版权声明:此博客为个人博客,不涉及商业用途,仅提供学习参考,内容均来自个人原创以及互联网转载和摘录。 --------------------- 本文来自 路西法Lucifer 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/qq_37495786/article/details/83514743

项目结果:

1、     项目所需的jar包,因为是SpringBoot的demo,所以这里只需要引入spring-boot-starter-parent和spring-boot-starter-web即可。

pom.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.springboot</groupId>
    <artifactId>scheduled-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>scheduled-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.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-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

1、启动类:加上@EnableScheduling,这样才会去扫描@Scheduled的方法,去掉之后@Scheduled的方法将不会被执行。

package com.springboot.scheduleddemo;

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

@SpringBootApplication
@EnableScheduling
public class ScheduledDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledDemoApplication.class, args);
    }
}

2、写定时任务:

参考:在线Cron表达式

@Scheduled() 注解中写入Cron表达式。

此处将类加上@Component注解或者@Service均可。

"0/1 * * * * ? "表示每秒执行一次

package com.springboot.scheduleddemo.service;

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

import java.util.Date;

/**
 * @author: Lucifer
 * @create: 2018-10-29 22:39
 * @description:
 **/
@Component
public class ScheduledService {

    @Scheduled(cron = "0/1 * * * * ? ")
    public void sayHello(){
        System.out.println("定时任务1:"+new Date());
    }

}

控制台:

3、加入SayNo方法

@Scheduled(cron = "0/1 * * * * ? ")
    public void sayNo(){
        System.out.println("定时任务2"+new Date()+".......No");
    }

此时启动,会看到控制台如下图:此时是同步的,串行,先执行sayHello,再执行SayNo.

4、加上@Async

@SpringBootApplication
@EnableScheduling
@Async
public class ScheduledDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledDemoApplication.class, args);
    }
}

再次查看控制台:如图

此时不再是按顺序去执行了,而是异步。在启动类加上@Async,表示执行的是异步的

猜你喜欢

转载自blog.csdn.net/qq_37495786/article/details/83514743