原文

调度任务

用spring实现一个任务调度。

你将做的

你将做一个应用每5秒钟打印当前时间,用@Scheduled注解。

你需要啥

  • 15分钟
  • 文本编辑器或者IDE
  • JDK1.8+
  • Gradle4+或Maven3.2+
  • 你可以把代码直接导入到IDE里面

如何完成

选择走Maven的方式

创建目录结构

通过mkdir -p 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 http://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.0.5.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>

spring-boot-maven-plugin插件。他提供了很多便捷的特性。

  • 把用到的所有依赖打包成一个整体,这样方便服务的执行以及分发。
  • public static void main()标记成可执行类。
  • 提供了内置的依赖解析器用于设置相符的Spring Boot依赖的版本号。

创建一个调度任务

目录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
*
* http://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()));
}
}

首先是@Component,这个属于Spring里面比较常见的注解。@Service@Repository@Controller,都属于@Component,用在特定的地方。他们都会在启动的时候被标记为Spring Bean。Bean可以理解为Spring的骨架,核心的东西。

@Scheduled用来指定特定的方法,例子中用的参数是fixedRate,表示是固定频率,每次执行的开始时间的间隔。其他类似的参数例如fixedDelay用来表示在上一次完成之后下次开始的间隔。你也可以用cron表达式。

启用调度

虽然调度任务可以放在一个war文件的web应用里面,下面用一种更简单的方式。打包成一个单独的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);
}
}

@SpringBootApplication是一种简写,做了几件事情。

  • 自动添加了@Configuration用来标记一个类是bean文件的来源类。
  • @EnableAutoConfiguration告诉SpringBoot依据包括classpath的设置,其他bean,各种属性的设置来添加bean。
  • 通常来说你需要添加@EableWebMvc用于SpringMvc的应用,但是SpringBoot会帮你自动添加如果在classpath里面看到spring-webmvc的。他会帮你设置DispatcherServlet
  • @ComponentScan告诉Spring去查找hello包里面的组件配置,controller。

main()方法使用SpringBoot的SpringApplication.run()方法来启动一个应用。是的,没有xml。

@EnableScheduling确保创建后台任务执行器。

构建一个可执行的jar

这里通过Maven来执行以及打包

  • 执行 mvn spring-boot:run

  • 打包成jar文件并执行

    打包:

    mvn clean package

    执行:

    java -jar target/gs-scheduling-tasks-0.1.0.jar

总结

  • Maven使用(定义,运行,打包)
  • 设置一个SpringBootApplication应用
  • EnableScheduling来启用任务调度

[SpringBoot guides系列翻译]调度任务的更多相关文章

  1. [SpringBoot guides系列翻译]调用RESTfulWebService

    原文 参考链接 CommandLineRunner Bean 翻译如何调用RESTful WebService 这节将演示如何在SpringBoot里面调用RESTful的WebService. 构建 ...

  2. [SpringBoot guides系列翻译]通过JDBC和Spring访问关系数据库

    原文 参考链接 hikaricp Spring Boot JDBC Starter Spring Boot Starter Parent h2 database introduction Autowi ...

  3. [SpringBoot guides系列翻译]SpringBoot构建RESTful程序入门

    原文地址 构建一个RESTful的WebService 这个指南将带你用Spring创建一个RESTful的helloworld程序. 你将完成 在下面地址上创建一个接收http get请求的服务 h ...

  4. [SpingBoot guides系列翻译]文件上传

    文件上传 这节的任务是做一个文件上传服务. 概况 参考链接 原文 thymeleaf spring-mvc-flash-attributes @ControllerAdvice 你构建的内容 分两部分 ...

  5. [SpingBoot guides系列翻译]Redis的消息订阅发布

    Redis的消息 部分参考链接 原文 CountDownLatch 概述 目的 这节讲的是用Redis来实现消息的发布和订阅,这里会使用Spring Data Redis来完成. 这里会用到两个东西, ...

  6. 【SpringBoot基础系列】手把手实现国际化支持实例开发

    [SpringBoot基础系列]手把手实现国际化支持实例开发 国际化的支持,对于app开发的小伙伴来说应该比价常见了:作为java后端的小伙伴,一般来讲接触国际化的机会不太多,毕竟业务开展到海外的企业 ...

  7. SpringBoot基础系列-SpringCache使用

    原创文章,转载请标注出处:<SpringBoot基础系列-SpringCache使用> 一.概述 SpringCache本身是一个缓存体系的抽象实现,并没有具体的缓存能力,要使用Sprin ...

  8. SpringBoot基础系列-使用日志

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996897.html SpringBoot基础系列-使用日志 概述 SpringBoot ...

  9. SpringBoot基础系列-使用Profiles

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996884.html SpringBoot基础系列-使用Profile 概述 Profi ...

随机推荐

  1. LVS+keepalived负载均衡

    背景:         随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高 ...

  2. 面试阿里前端P6血和泪换来的收获

      我的一个朋友在前端耕耘一段时间,也在网上进行了高度培训学习,最近一段时间他打算跳槽去阿里面试前端P6开发岗位,结果被痛虐了一回,估计从此以后会给他留下不可磨灭的阴影啊 真是十年生死两茫茫,一鲁代码 ...

  3. Python并发编程之深入理解yield from语法(八)

    大家好,并发编程 进入第八篇. 直到上一篇,我们终于迎来了Python并发编程中,最高级.最重要.当然也是最难的知识点--协程. 当你看到这一篇的时候,请确保你对生成器的知识,有一定的了解.当然不了解 ...

  4. Java基础-递归调用

    意义: 递归算法是一种直接或间接地调用自身的算法.在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于理解. 所以在工作中我们经常用递归 来进行一些算法操作 比如 ...

  5. Jmeter-----【mac电脑】配置web浏览器的代理抓取请求

    在测试中,不仅会涉及到APP中的数据测试,时常我们APP的数据需要与后台进行交互,因此我们不可避免的也需要对web进行接口测试,更准确的来说是使用web的接口来快速的帮我们实现App中所需的数据录入, ...

  6. 对于单页应用中如何监听 URL 变化的思考

    周末开发了一个在 GitHub 中给 repo 增加自定义备注的 chrome 扩展. 开发这个扩展的原因是我在 GitHub 中所 star 的项目实在太多了(截止目前 671 个),有的项目过个几 ...

  7. Bootstrap优秀模板-Unify.2.6.2

    这是一个非常老牌的Bootstrap商业模板,全面性和稳定性俱佳,有LandingPage.BussinessPage.AdminPage多种模式,非常推荐用来构建官网.响应式应用Web.管理端Web ...

  8. IE不兼容ES6箭头函数的解决方法(在浏览器中使用)

    polyfill.js下载方法: npm install babel-polyfill --save 页面中引用"polyfill.js" 和 "browser.min. ...

  9. 强化学习(十五) A3C

    在强化学习(十四) Actor-Critic中,我们讨论了Actor-Critic的算法流程,但是由于普通的Actor-Critic算法难以收敛,需要一些其他的优化.而Asynchronous Adv ...

  10. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...