springboot - 应用实践(N)使用springboot内置的@Scheduled
1、springboot开箱即用,内置调度任务的使用。
建一个简单的springboot工程,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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lfy</groupId>
<artifactId>javatestproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>javatestproject</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2、使用@Scheduled
package com.lfy.javatestproject.start; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class TaskDemo { /**
* 每5秒执行一次
*/
@Scheduled(cron = "0/5 * * * * ? ")
public void sayHello(){
System.out.println("Hello.");
} /**
* 每2秒执行一次
*/
@Scheduled(fixedRate = 2000)
public void sayHappy(){
System.out.println("I am Happy.");
}
}
3、将启动项注解@EnableScheduling
package com.lfy.javatestproject; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling
@SpringBootApplication
public class JavatestprojectApplication { public static void main(String[] args) {
SpringApplication.run(JavatestprojectApplication.class, args);
} }
4、运行查看
"C:\Program Files\Java\jdk1.8.0_65\bin\java.exe" -XX:TieredStopAtLevel= -noverify -Dspring.output.ansi... . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE) -- ::31.403 INFO --- [ main] c.l.j.JavatestprojectApplication : Starting JavatestprojectApplication on DESKTOP-V80IF6T with PID ...
-- ::31.418 INFO --- [ main] c.l.j.JavatestprojectApplication : No active profile set, falling back to default profiles: default
-- ::31.935 INFO --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): (http)
-- ::31.951 INFO --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
-- ::31.951 INFO --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.]
-- ::31.998 INFO --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
-- ::31.998 INFO --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in ms
-- ::32.107 INFO --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
-- ::32.185 INFO --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
I am Happy.
-- ::32.216 INFO --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): (http) with context path ''
-- ::32.216 INFO --- [ main] c.l.j.JavatestprojectApplication : Started JavatestprojectApplication in 1.033 seconds (JVM running for 1.53)
I am Happy.
Hello.
I am Happy.
I am Happy.
Hello.
I am Happy.
I am Happy.
I am Happy.
Hello.
I am Happy.
I am Happy.
Hello.
I am Happy.
I am Happy.
I am Happy.
Hello.
I am Happy.
springboot - 应用实践(N)使用springboot内置的@Scheduled的更多相关文章
- SpringBoot(十四)_springboot使用内置定时任务Scheduled的使用(一)
为什么使用定时? 日常工作中,经常会用到定时任务,比如各种统计,并不要求实时性.此时可以通过提前设置定时任务先把数据跑出来,后续处理起来更方便. 本篇文章主要介绍 springboot内置定时任务. ...
- Springboot入门及配置文件介绍(内置属性、自定义属性、属性封装类)
目的: 1.Springboot入门 SpringBoot是什么? 使用Idea配置SpringBoo使用t项目 测试案例 2.Springboot配置文件介绍 内置属性 自定义属性 属性封装类 Sp ...
- 实践解析丨Rust 内置 trait:PartialEq 和 Eq
摘要:Rust 在很多地方使用了 traits, 从非常浅显的操作符重载, 到 Send, Sync 这种非常微妙的特性. Rust 在很多地方使用了 traits, 从非常浅显的操作符重载, 到 S ...
- SpringBoot源码篇:Spring5内置tomcat实现code-based的web.xml实现
一.简介 上篇文章讲了SpingBoot诞生的历史背景和技术演进背景,并通过源码说明了SpringBoot是如何实现零配置的包括如何省去web.xml配置的原理.本文接上一篇文章,通过demo演示Sp ...
- springboot+内置改为外置tomcat
1.pom.xml springboot项目利用的是自己内置的tomcat,这边就是不依赖内置的tomcat,将其编译的作用域设置为provided <dependency> <gr ...
- SpringBoot内置的各种Starter是怎样构建的?--SpringBoot源码(六)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 温故而知新 本篇接 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五) 温 ...
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
前言:本项目基于maven构建 spring-boot项目可以快速构建web应用,其内置的tomcat容器也十分方便我们的测试运行: spring-boot项目需要部署在外部容器中的时候,spring ...
- Spring-Boot 内置静态资源文件地址修改
Spring-Boot 内置MVC静态文件地址修改 Why:1.Spring-Boot修改内置SpringMVC静态资源路径,提高项目目录结构的安全性.2.配置拦截路径时可以剔除静态文件拦截How:1 ...
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...
随机推荐
- 29. ClustrixDB 分布式架构/并发控制
介绍 ClustrixDB使用多版本并发控制(MVCC)和2阶段锁(2PL)的组合来支持混合的读写工作负载.在我们的系统中,读取器享受无锁快照隔离,而写入器使用2PL来管理冲突.并发控制的组合意味着读 ...
- Codeforces 932 E Team Work ( 第二类斯特林数、下降阶乘幂、组合数学 )
题目链接 题意 : 其实就是要求 分析 : 先暴力将次方通过第二类斯特林数转化成下降幂 ( 套路?) 然后再一步步化简.使得最外层和 N 有关的 ∑ 划掉 这里有个技巧就是 将组合数的表达式放到一边. ...
- 小程序上传wx.uploadFile - 小程序请假-请求
小程序上传wx.uploadFile UploadTask wx.uploadFile(Object object) 将本地资源上传到服务器.客户端发起一个 HTTPS POST 请求,其中 cont ...
- sql 建立索引之前计算区分度
select cutomer_id,title,content from product_comment where audit_status=1 and product_id=1 and produ ...
- Horizon7.9部署和克隆问题汇总
1 基础环境说明 采用Windows server +SQL Server 2014进行部署,对接现有环境中的AD预控,系统版本为Windows server .桌面虚拟化软件版本采用Horizon ...
- WebView的基础用法
新建一个WebView项目,然后修改activity_main.xml布局文件中的代码,如下所示: <LinearLayout xmlns:android="http://schema ...
- 【C++】C++类的static 关键字理解
转载自:ZJE_ANDY static修饰类中成员,表示类的共享数据 1.static类成员 C++primer里面说过,static类成员不像普通的类数据成员,static类数据成员独立于一切类对象 ...
- CSS样式属性单词之Left
通常left单独在CSS中设置无效,需要在使用position属性下使用才能生效起作用.left左靠多少距离(距离左边多少)的作用. left 一.left认识与语法 left翻译:左边,左 在CSS ...
- failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED 错误解决方法
解决: config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=con ...
- [Flask]jinja2渲染分页导航部件
注意: 1.在视图函数中通过request.args.get('page')获取page数,并将page传给macros.html模板文件 效果: 点击8,就跳转到第8页数据了 视图函数 @app.r ...