Spring Boot 内置定时任务
启用定时任务
@SpringBootApplication
@EnableScheduling // 启动类添加 @EnableScheduling 注解
public class ScheduleDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleDemoApplication.class, args);
}
}
新增定时任务类
@Component // 类上添加 @Component 注解
public class TaskDemo {
private static final Logger logger = LoggerFactory.getLogger(TaskDemo.class);
@Scheduled(cron = "0/5 * * * * ? ") // 方法上添加 @Scheduled 注解
public void job1(){
try {
logger.info("job1");
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Scheduled(cron = "0/5 * * * * ? ")
public void job2(){
try {
logger.info("job2");
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
多线程执行
从上面图片可以看到开启多个任务是以单线程执行的,执行完当前任务才会继续执行下一个
启用多线程执行有两种方式:
使用默认线程池
@Component
@EnableAsync // 类上添加 @EnableAsync 注解
public class TaskDemo {
... ...
@Async // 方法上添加 @Async 注解
@Scheduled(cron = "0/5 * * * * ? ")
public void job1(){
... ...
}
... ...
}
使用自定义线程池
添加配置类:
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(2);
threadPoolTaskScheduler.setThreadNamePrefix("my-pool-");
threadPoolTaskScheduler.initialize();
scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
参考
Spring Boot 内置定时任务的更多相关文章
- Spring boot 内置tomcat禁止不安全HTTP方法
Spring boot 内置tomcat禁止不安全HTTP方法 在tomcat的web.xml中可以配置如下内容,让tomcat禁止不安全的HTTP方法 <security-constraint ...
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
一.概念 1.事件监听的流程 步骤一.自定义事件,一般是继承ApplicationEvent抽象类 步骤二.定义事件监听器,一般是实现ApplicationListener接口 步骤三.启动时,需要将 ...
- 自定义Spring Boot内置tomcat的404页面
spring boot 的相关404页面配置都是针对项目路径下的(如果配置了 context-path) 在context-path不为空的情况下,如果访问路径不带context-path,这时候会显 ...
- Spring boot内置Tomcat的临时目录被删除导致文件上传不了-问题解析
目录 1.问题 2.1. 为什么需要使用这个/tmp/tomcat*? 2.2.那个 /tmp/tomcat* 目录为什么不存在? 三.解决办法 修改 springboot 配置,不要在/tmp 下创 ...
- Spring Boot内置Tomcat
Spring Boot默认支持Tomcat/Jetty/Undertow作为底层容器.在之前实战相关的文章中,可以看到引入spring-boot-starter-web就默认使用tomcat容器,这是 ...
- 配置spring boot 内置tomcat的accessLog日志
#配置内置tomcat的访问日志server.tomcat.accesslog.buffered=trueserver.tomcat.accesslog.directory=/home/hygw/lo ...
- SpringBoot(十四)_springboot使用内置定时任务Scheduled的使用(一)
为什么使用定时? 日常工作中,经常会用到定时任务,比如各种统计,并不要求实时性.此时可以通过提前设置定时任务先把数据跑出来,后续处理起来更方便. 本篇文章主要介绍 springboot内置定时任务. ...
- 【spring boot】spring boot中使用定时任务配置
spring boot中使用定时任务配置 =============================================================================== ...
- Spring MVC内置支持的4种内容协商方式【享学Spring MVC】
每篇一句 十个光头九个富,最后一个会砍树 前言 不知你在使用Spring Boot时是否对这样一个现象"诧异"过:同一个接口(同一个URL)在接口报错情况下,若你用rest访问,它 ...
随机推荐
- Ordering Cows
题意描述 好像找不到链接(找到了请联系作者谢谢),所以题目描述会十分详细: Problem 1: Ordering Cows [Bruce Merry, South African Computer ...
- python开发基础(二)运算符以及数据类型之int(数字)
# encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...
- 19、Haystack
Haystack 1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsear ...
- 记载idea创建spring-boot项目时“Spring Initalizr Error”的问题处理
- 企业中真实需要的集中管理软件SVN即Subversion版本控制
一.SVN基本概念 SVN是Subversion的简称,是一个自由开源的版本控制系统. checkout: 把整个项目源码下载到本地 update: 从服务器上更新代码,使本地达到最新版本 commi ...
- kubernetes-1.18.2集群安装-02
一.基础配置 修改主机名 # 在 172.17.32.23 上:hostnamectl set-hostname k8s-master01bash# 在 172.17.32.38 上:hostnam ...
- svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方案
今天SVN提交代码遇到"Previous operation has not finished; run 'cleanup' if it was interrupted"报错,&q ...
- centos6.5开机执行命令
虚拟机由于用nat方式联网centos6.5,设置了eth0 dhcp,开机没自动获取到IP, 导致secureCRT连不上,所以,让linux开机自动执行下dhclient获取下分配的ip, 这样, ...
- 【JVM第六篇--对象】对象的实例化、内存布局和访问定位
写在前面的话:本文是在观看尚硅谷JVM教程后,整理的学习笔记.其观看地址如下:尚硅谷2020最新版宋红康JVM教程 一.对象的实例化 在平常写代码的过程中,我们用class关键字定义的类只是一个类的模 ...
- 如何删除一台OSD主机
在ceph的一台OSD主机出现故障的时候,数据可以通过副本的机制进行恢复,之后通过删除osd的操作也能够将故障osd从osd tree当中删除掉,但是故障的 osd 的主机仍然会留在集群当中,通过 c ...