在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容。

如果我们不用springboot开发的话,我们写定时任务需要写那些配置呢?

我们需要在application.xml文件中添加以下配置:

1.在<beans   ..  />中添加

  xmlns:tx="http://www.springframework.org/schema/tx"

  还有xsi:schemaLocation =“...”中添加

    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.3.xsd

  这就算引进task任务的功能了,接着,我们要开启task任务,配置

    <task:annotation-driven />

  然后在使用定时任务的类名上面添加注解@Component交由spring来管理,对不对。

  最后在方法名上面添加注解@Scheduled(cron="*/6 * * * * ?") 或者@Scheduled(fixedRate = 6000),这样基本就完成了。

  这样做并不算很复杂和繁琐。

那现在用springboot开发,我们没有xml配置文件了。我们怎么做?

springboot默认已经帮我们实现了xml文件中的一套配置,只需要添加相应的注解就可以实现。

pom.xml

首先在pom里面添加包含定时任务的依赖包。

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

application.java

在启动类上面加上@EnableScheduling即可开启定时。

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

创建定时任务实现类

@Component
public class SchedulerTask { private int count=0; @Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+(count++));
} }

或者

@Component
public class Scheduler2Task { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 6000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
} }

参数说明

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。

fixedRate 说明

  • @Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行
  • @Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次

springboot(八)-定时任务的更多相关文章

  1. 玩转SpringBoot之定时任务详解

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  2. SpringBoot 配置定时任务

    SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...

  3. SpringBoot - 添加定时任务

    SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...

  4. springboot之定时任务

    定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...

  5. SpringBoot整合定时任务和异步任务处理 3节课

    1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类        ...

  6. 十三、springboot集成定时任务(Scheduling Tasks)

    定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...

  7. SpringBoot创建定时任务

    之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1 ...

  8. springboot开启定时任务 添加定时任务 推送

    最近在自学Java的springboot框架,要用到定时推送消息.参考了网上的教程,自己调试,终于调好了.下面将网上的教程归纳下,总结复习下.  springboot开启定时任务  在SpringBo ...

  9. (入门SpringBoot)SpringBoot结合定时任务task(十)

    SpringBoot整合定时任务task 使用注解EnableScheduling在启动类上. 定义@Component作为组件被容器扫描. 表达式生成地址:http://cron.qqe2.com ...

随机推荐

  1. Spring 实例化bean的三种方式

    第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...

  2. 数据标准化方法及其Python代码实现

    数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间.目前数据标准化方法有多种,归结起来可以分为直线型方法(如极值法.标准差法).折线型方法(如三折线法).曲线型方法 ...

  3. wamp安装两个,数据库丢了,怎么办

    wampserver3.*下载了好几天一直没有安装,今天发现必须安装,已升级自己的php版本,不过也饿可以自己手动配置PHP版本,既然有安装包就算了吧,当安装完后,发现忘记备份自己的数据库了,幸好之前 ...

  4. LINUX 查看CUP温度

    在Linux下可以通过lm_sensors来查看CPU的温度(当然你的硬件首先要支持),要使用这个功能要有内核相关模块(比如I2C)的支持,下面说一下操作方法: 先看一下你的机器上是否安装了lm_se ...

  5. Examining Application Startup in ASP.NET 5

    By Steve Smith  June 23, 2015 ASP.NET 5 differs from previous versions of ASP.NET in many ways. Gone ...

  6. XE中FMX操作ListBox,添加上千条记录(含图片)

    我之前是想在ListBox的每个Item上添加一个图片,Item上所有的内容都是放在Object里赋值,结果发现加载一百条记录耗时四五秒: procedure TMainForm.AddItem; v ...

  7. linux系统下ipmitool添加BMC帐号密码

    需求:已知BMC帐号id2为root管理员帐号,添加id5bmc帐号 工具:ipmitool version 1.8.14 系统:CentOS release 6.6 (Final) 1,通过yum安 ...

  8. 「BZOJ 2733」「HNOI 2012」永无乡「启发式合并」

    题意 你需要维护若干连通快,有两个操作 合并\(x,y\)所在的连通块 询问\(x\)所在连通块中权值从小到大排第\(k\)的结点编号 题解 可以启发式合并\(splay\),感觉比较好些的 一个连通 ...

  9. python爬取淘宝排名

    import timeimport jsonimport requestsimport xlrdimport randomimport os from xlutils.copy import copy ...

  10. 【spring】@Qualifier注解

    近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...