开发基础
开发基础笔记
mybatis-plus通用的service方法
通过jenkins配置前后端自动打包及发布
mybatis-plus实现分页的方式
Java极客技术公众号PDF
狂神说SpringBoot
Hashids java 版使用
SpringBoot
1、Web快速开发
2、结果集的封装
3、集成MyBatis实现数据库操作
4、Springboot @Validated参数校验
5、SpringBoot全局异常处理
6、拦截器HandlerInterceptor
7、集成Swagger实现API自动生成
8、集成knife4j实现API自动生成
9、Springboot集成MyBatis-Plus快速入门
10、springboot自定义注解及AOP切面使用
11、使用Shiro实现登陆和权限认证,基于MyBatis
12、集成SpringSecurity实现授权认证
13、SpringBoot集成EasyExcel实现数据导入与导出
14、Spring Task定时任务的实现
15、Quartz快速上手与实践
16、如何用代码实现Spring IOC
17、SpringBoot集成JWT,实现接口的鉴权交互
SpringCloud
Nacos作为服务注册中心
seata1.6.1 结合springcloud实现分布锁的技术笔记
一些技术博客推荐
前端面试相关
看这一篇就够了
java.util包常用的类和接口
CountDownLatch介绍与使用
Common-lang3使用入门
Hutool简单使用入门
lombok 介绍及基本使用方法
git项目统计成员代码行数和提交的次数
mysql 逗号分隔的数据 like查询
使用sonar进行代码质量检查
线上使用jmeter进行压测的时候,使用Arthas诊断工具排查响应慢的接口
php结合phpstudy8、vscode开启xdebug进行代码调试
node-red使用入门
本文档使用 MrDoc 发布
-
+
首页
14、Spring Task定时任务的实现
在Java中开发定时任务主要有三种方案:一是使用JDK自带的Timer,二是使用第三方组件Quartz',三是使用Spring Task。 ### 1、启动类上添加@EnableScheduling ```java @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class,args); } } ``` ### 2、支持两种方式的定时 #### 2.1 **fixedRate定时** 每2秒执行一次 ```java @Component public class TaskTest { /** * 每2秒执行1次 */ @Scheduled(fixedRate = 2000) public void fixedRateMethod() throws InterruptedException { System.out.println("task-fixedRate,每2秒执行1此:" + new Date()); Thread.sleep(1000); } } ``` 通过cron表达示的方式 ```java @Component public class TaskCron { private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 在每分钟的00秒执行 */ @Scheduled(cron = "0 * * * * ?") public void oneMin() throws InterruptedException { System.out.println("cron每分钟执行:" + simpleDateFormat.format(new Date())); } /** * 每5秒执行 */ @Scheduled(cron = "*/5 * * * * ?") public void fiveS() throws InterruptedException { System.out.println("cron每5秒执行:" + simpleDateFormat.format(new Date())); } } ```
superadmin
2023年11月3日 17:20
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码