个人博客网:https://wushaopei.github.io/    (你想要这里多有)

1、创建具体要执行的任务类:

package com.example.poiutis.timer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.TimerTask; /**
* @ClassName MyTimeTask
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 15:55
* @Version 1.0
*/
public class MyTimeTask extends TimerTask{ private static Logger logger = LoggerFactory.getLogger(MyTimeTask.class); private String name;
public MyTimeTask(String inputName){
name = inputName;
}
@Override
public void run() {
//打印当前name 的内容
System.out.println("Current exec name is " + name);
logger.info(System.currentTimeMillis()+"111");
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

run()方法中是要执行的任务代码,定时器启动时会执行 run() 方法中的业务逻辑 ;

2、创建 timer 的 实例工作类:

package com.example.poiutis.timer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger; import java.sql.Time;
import java.util.Timer; /**
* @ClassName MyTimer
* @Description TODO
* @Author wushaopei
* @Date 2019/7/26 15:57
* @Version 1.0
*/
@Configuration
public class MyTimer {
// public static void main(String[] args) {
@Bean
public void testQuartzTrigger1() {
//1.创建一个timer实例
Timer timer = new Timer();
//2.创建一个MyTimerTask实例
MyTimeTask myTimeTask = new MyTimeTask("No.1"); //3.通过timer定时定频率调用myTimerTask的业务逻辑
// 即 第一次执行是在当前时间的两秒之后,之后每隔一秒钟执行一次\
timer.schedule(myTimeTask,2000L,1000L); }
}

添加@Configuration  注解,自动注入实例对象,并由springboot 启动 定时器,执行任务。

注意: 使用springboot 时保证包扫描路径是正确的;

执行效果:

GitHub

Spring Boot笔记(六) springboot 集成 timer 定时任务的更多相关文章

  1. Spring Boot笔记(三) springboot 集成 Quartz 定时任务

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1. 在 pom.xml 中 添加 Quartz 所需要 的 依赖 <!--定时器 quartz- ...

  2. Spring Boot笔记(二) springboot 集成 SMTP 发送邮件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...

  3. Spring Boot笔记(一) springboot 集成 swagger-ui

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.添加依赖 <!--SpringBoot整合Swagger-ui--> <depen ...

  4. Spring Boot笔记(五) SpringBoot 集成Lombok 插件

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 为了减少代码量,为当前项目添加 lombok 来优雅编码 Lombok 插件安装: a . 添加依赖: ...

  5. Spring Boot(六)集成 MyBatis 操作 MySQL 8

    一.简介 1.1 MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集. ...

  6. Spring Boot笔记六:Thymeleaf介绍

    目录 什么是thymeleaf? 创建最简单的thymeleaf thymeleaf语法 什么是thymeleaf? thymeleaf是一个模板引擎,是用来在Spring Boot中代替JSP的 引 ...

  7. Spring Boot 2.X(十二):定时任务

    简介 定时任务是后端开发中常见的需求,主要应用场景有定期数据报表.定时消息通知.异步的后台业务逻辑处理.日志分析处理.垃圾数据清理.定时更新缓存等等. Spring Boot 集成了一整套的定时任务工 ...

  8. spring boot / cloud (六) 开启CORS跨域访问

    spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...

  9. Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏

    Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏 云收藏项目已经开源3年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时 ...

随机推荐

  1. 【Hadoop离线基础总结】日志采集框架Flume

    日志采集框架Flume Flume介绍 概述 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统.它可以采集文件,socket数据包.文件.文件夹.kafka等各种形式源数据,又可 ...

  2. DoNet:浅淡对delegate的理解

    1 前言 C#的相关文档,MSDN上其实已经很详细了,关于delegate的使用可以参 考MSDN上的文档https://msdn.microsoft.com/zh-cn/library/900fyy ...

  3. Properties集合与IO流

    package com.itheima.demo07.Prop; import java.io.FileOutputStream; import java.io.FileReader; import ...

  4. sqli-labs之Page-3

    第三十八关:堆叠注入 $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1"; /* execute multi query */ ...

  5. SpringBoot 整合SpringBatch实际项目改造

    SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fo ...

  6. python实现简易词频统计-源码

    需求:给瓦尔登湖文章统计单词出现的频率 思路:首先读取文件并以空格分割得到列表,然后利用for循环遍历列表中的元素并把去掉列表元素中的符号,第三步去掉相同的元素,将列表转换为一个字典,最后按照键值对升 ...

  7. mybatis中的动态SQL(IF Chooes When Where Set ForEach SQL片段)

    mapper: public interface BlogMapper { List<Blog> getBlogByIF(Map map); } IF <select id=&quo ...

  8. 机器学习必会工具gensim

    import jieba import gensim from gensim import corpora from gensim import models from gensim import s ...

  9. zabbix监控redis多实例cpu mem-自动发现

    1.自动发现实例端口脚本,用于zbx item prototypes #!/bin/bash REDIS_PORT=`ps aux |grep redis-server | grep -v 'grep ...

  10. 实验三 Java基本程序设计(2)

                                             实验三 Java基本程序设计(2)                                           ...