[原创]2.定时任务

作者: 哈士奇18 | 来源:发表于2019-07-24 22:58 被阅读0次

学习spring如何创建定时任务.

实现效果:

构建一个应用程序,使用Spring@Scheduled注解每五秒打印一次当前时间。

项目结构
└── src

    └── main

        └── java

            └── hello
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-scheduling-tasks</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

</project>

1.创建一个定时任务

  • 创建一个定时任务类
    src/main/java/hello/ScheduledTasks.java
/*
 * Copyright 2012-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hello;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

@Scheduled注解定义了reportCurrentTime()方法何时运行.备注:本例用的是fixedRate选项,它指定了方法调用的时间间隔包括程序执行时间在内,单线程有可能遇到阻塞.
还有其他选项,例如fixedDelay,它指定时间间隔不包括程序执行时间。 还可以使用@Scheduled(cron =“...”)表达式进行更复杂的任务调度

2.运行

虽然计划任务可以嵌入到Web应用程序和WAR文件中,但下面演示的更简单的方法创建了一个独立的应用程序。 将所有内容打包在一个可执行的JAR文件中,由一个普通的 main()方法驱动
src/main/java/hello/Application.java

package hello;

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

@SpringBootApplication
@EnableScheduling
public class Application {

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

@EnableScheduling注解保证能够创建后台可执行定时任务程序。 没有它,不能创建定时任务。

建立一个可执行的 JAR

利用maven打包war或者jar运行,这里不做介绍.
java -jar target/gs-rest-service-0.1.0.jar

测试

运行程序后,显示日志输出,可以从控制台的日志中看到 每5秒钟执行一次方法,输出类似下面的代码:

[...]
2016-08-25 13:10:00.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:00
2016-08-25 13:10:05.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:05
2016-08-25 13:10:10.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:10
2016-08-25 13:10:15.143  INFO 31565 --- [pool-1-thread-1] hello.ScheduledTasks : The time is now 13:10:15

相关文章

  • [原创]2.定时任务

    学习spring如何创建定时任务. 实现效果: 构建一个应用程序,使用Spring的@Scheduled注解每五秒...

  • Day-23 定时任务

    1.什么是定时任务 某个时间点定时执行一个任务 2.定时任务的使用场景 1.日志切割2.电商定时秒杀3.定时备份4...

  • Android中 Handler延时 定时任务

    1.延时 2.定时任务,间隔固定时间执行某项任务 3.定时任务,间隔固定时间执行某项操作后关闭定时任务 参考:ht...

  • 2019-07-30

    day22 操作系统定时任务 课程介绍 1.定时任务概念介绍2.定时任务实现方式3.定时任务配置应用过程4.定时...

  • Spring Boot定时任务

    1.Scheduled定时任务器2.整合Quartz定时任务框架3.cron表达式 1.Scheduled定时任务...

  • 定时任务

    1.设置定时任务可以定时启动特定任务 2.定时任务需可手动启动 3.如果定时任务跑的数据有问题,如何修复? 删除错...

  • 23笔记---定时任务

    今日内容 定时任务1.什么是定时任务2.为什么用定时任务3.定时任务使用情况4.定时任务如何使用5.书写时的规范 ...

  • crontab创建定时任务

    1. 创建一个定时任务的文件 输入 2. 开始定时任务 3. 查看正在运行的定时任务 4. 查看定时任务执行结果 ...

  • Day 23 定时任务

    1.什么是定时任务 定点执行,某个job. 2.为什么用定时任务? 3.定时任务使用情况? 4.定时任务如何使用?...

  • 第二十三天 定时任务

    第二十三天 定时任务 定时任务 1.什么是定时任务 定点执行,某个工作 2.为什么用定时任务 电商秒杀,定点开启,...

网友评论

    本文标题:[原创]2.定时任务

    本文链接:https://www.haomeiwen.com/subject/jxrqrctx.html