Spring boot 集成三种定时任务方式
三种定时任务方式分别为
- org.springframework.scheduling.annotation.Scheduled
- java.util.concurrent.ScheduledExecutorService
- java.util.Timer
示例如下:
1. 新建Maven项目 schedule
2. pom.xml
- <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.java</groupId>
- <artifactId>schedule</artifactId>
- <version>1.0.0</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.5.RELEASE</version>
- </parent>
- <dependencies>
- <!-- Spring Boot -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- 热部署 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>springloaded</artifactId>
- <version>1.2.8.RELEASE</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>${project.artifactId}</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
3. ScheduleStarter.java
- package com.java;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.scheduling.annotation.EnableScheduling;
- @EnableScheduling
- @SpringBootApplication
- public class ScheduleStarter {
- public static void main(String[] args) {
- SpringApplication.run(ScheduleStarter.class, args);
- }
- }
4. ScheduledDemo.java
- package com.java.schedule;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- @Component
- public class ScheduledDemo {
- @Scheduled(cron = "0/5 * * * * *")
- public void demoTask() {
- System.out.println("[ ScheduledDemo ]\t\t" + System.currentTimeMillis());
- }
- }
5. ScheduledExecutorDemo.java
- package com.java.schedule;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.ScheduledThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import org.springframework.stereotype.Component;
- @Component
- public class ScheduledExecutorDemo {
- private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(5);
- @PostConstruct
- public void demoTask() {
- executor.scheduleAtFixedRate(new Runnable() {
- @Override
- public void run() {
- System.out.println("[ ScheduledExecutorDemo ]\t" + System.currentTimeMillis());
- }
- }, 0, 5, TimeUnit.SECONDS);
- }
- @PreDestroy
- public void destory() {
- executor.shutdown();
- }
- }
6. TimerDemo.java
- package com.java.schedule;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import org.springframework.stereotype.Component;
- @Component
- public class TimerDemo {
- private Timer timer = new Timer(true);
- @PostConstruct
- public void demoTask() {
- timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- System.out.println("[ TimerDemo ]\t\t\t" + System.currentTimeMillis());
- }
- }, 0, 5000);
- }
- @PreDestroy
- public void destory() {
- timer.cancel();
- }
- }
7. 运行ScheduleStarter.java,启动测试
控制台每隔5秒打印如下信息
- [ ScheduledDemo ] 1548156125005
- [ TimerDemo ] 1548156127556
- [ ScheduledExecutorDemo ] 1548156127566
三种定时任务都正常运行!
.
Spring boot 集成三种定时任务方式的更多相关文章
- Spring boot 集成三种拦截方式
三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- Spring Boot 项目几种启动方式
Spring Boot 项目几种启动方式 1. 使用 main 启动 jar xxxx.jar 2. 使用 mvn 启动 mvn spring-boot:run 3. 使用 Spring Boot c ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
- Spring IOC 中三种注入方式
项目错误知识点记录 正文 最近在项目的时候,用到Spring框架,Spring框架提供了一种IOC的自动注入功能,可以很轻松的帮助我们创建一个Bean,这样就省的我们四处写new Object()这样 ...
- Spring Boot集成quartz实现定时任务并支持切换任务数据源
org.quartz实现定时任务并自定义切换任务数据源 在工作中经常会需要使用到定时任务处理各种周期性的任务,org.quartz是处理此类定时任务的一个优秀框架.随着项目一点点推进,此时我们并不满足 ...
- Spring:Spring-IOC三种注入方式、注入不同数据类型
一.Spring IOC(依赖注入的三种方式): 1.Setter方法注入 package com.jpeony.spring.setter; import com.jpeony.spring.com ...
- Spring常用的三种注入方式
好文要收藏,摘自:https://blog.csdn.net/a909301740/article/details/78379720 Spring通过DI(依赖注入)实现IOC(控制反转),常用的注入 ...
- Spring Boot实践——三种拦截器的创建
引用:https://blog.csdn.net/hongxingxiaonan/article/details/48090075 Spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可 ...
随机推荐
- Java-IO读写文件简单操作2
承接Java-IO读写文件简单操作,这里再次写个小demo巩固一下知识点. 代码文件:demo.java package com.test.demo; import java.io.*; public ...
- 软件使用---Eclipse
代码提示快捷操作.这个叫做,内容分析(content assist) 1.设置自动提示: 2.设置快捷键:
- oracle 11g 将非分区表转换为分区表在线重定义
--操作的用户需要有以下的权限 GRANT CONNECT, RESOURCE TO CMIGDW; GRANT EXECUTE ON DBMS_REDEFINITION TO CMIGDW; GRA ...
- HIbernate基于外键的查询
此文以个人开发记录为目的,笔拙勿喷 项目是背景是公司的E签宝平台VIP频道项目进行关联账户增加后,需要做删除时的,联合查询 当前主要表结构账户表Account. CREATE TABLE `accou ...
- css内容整理2
10.6.css伪类.伪元素 伪类用于向某些选择器添加特殊效果:伪元素用于将特殊的效果添加达到某选择器. 区别:伪类的效果可通过添加一个实际的类达到,用::伪元素效果则需要添加一个实际的元素,用:: ...
- powerdesigner低版本打开高版本方式为只读导致无法保存PD只读read-only-mode
由于版本号不一致 打开PD文件后提示: 点击[确定]后打开,点击[取消]后打不开 但打开后修改完毕保存提示: 解决办法: pdm文件实际上是个xml文件,直接用文本编辑器打开该文件修改版本号即可 把根 ...
- Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期
课程链接: 本节主要讲了三大块内容 1 bean的生命周期概念 2 bean的初始化和销毁的三种方式对比(代码演练) 3 总结 1 bean的生命周期概念 1.1 bean的定义:xml中关于bean ...
- Java I/O模型
本文转发自技术世界,原文链接 http://www.jasongj.com/java/nio_reactor/ 同步 vs. 异步 同步I/O 每个请求必须逐个地被处理,一个请求的处理会导致整个流程的 ...
- php赋值运算符
= 赋值 += $x+=3相当于$x = $x+3; -= *= /+ %= .=
- Lync二次开发关于Lync启动退出问题
以前使用C++开发的version.dll文件,由于各个用户环境的不同,造成某些用户加载不了我们开发的插件,并且写version.dll的同事还没找到好的解决办法,所以得换一种思路去解决这个问题,就是 ...