启用定时任务

@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 内置定时任务的更多相关文章

  1. Spring boot 内置tomcat禁止不安全HTTP方法

    Spring boot 内置tomcat禁止不安全HTTP方法 在tomcat的web.xml中可以配置如下内容,让tomcat禁止不安全的HTTP方法 <security-constraint ...

  2. 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件

    一.概念 1.事件监听的流程 步骤一.自定义事件,一般是继承ApplicationEvent抽象类 步骤二.定义事件监听器,一般是实现ApplicationListener接口 步骤三.启动时,需要将 ...

  3. 自定义Spring Boot内置tomcat的404页面

    spring boot 的相关404页面配置都是针对项目路径下的(如果配置了 context-path) 在context-path不为空的情况下,如果访问路径不带context-path,这时候会显 ...

  4. Spring boot内置Tomcat的临时目录被删除导致文件上传不了-问题解析

    目录 1.问题 2.1. 为什么需要使用这个/tmp/tomcat*? 2.2.那个 /tmp/tomcat* 目录为什么不存在? 三.解决办法 修改 springboot 配置,不要在/tmp 下创 ...

  5. Spring Boot内置Tomcat

    Spring Boot默认支持Tomcat/Jetty/Undertow作为底层容器.在之前实战相关的文章中,可以看到引入spring-boot-starter-web就默认使用tomcat容器,这是 ...

  6. 配置spring boot 内置tomcat的accessLog日志

    #配置内置tomcat的访问日志server.tomcat.accesslog.buffered=trueserver.tomcat.accesslog.directory=/home/hygw/lo ...

  7. SpringBoot(十四)_springboot使用内置定时任务Scheduled的使用(一)

    为什么使用定时? 日常工作中,经常会用到定时任务,比如各种统计,并不要求实时性.此时可以通过提前设置定时任务先把数据跑出来,后续处理起来更方便. 本篇文章主要介绍 springboot内置定时任务. ...

  8. 【spring boot】spring boot中使用定时任务配置

    spring boot中使用定时任务配置 =============================================================================== ...

  9. Spring MVC内置支持的4种内容协商方式【享学Spring MVC】

    每篇一句 十个光头九个富,最后一个会砍树 前言 不知你在使用Spring Boot时是否对这样一个现象"诧异"过:同一个接口(同一个URL)在接口报错情况下,若你用rest访问,它 ...

随机推荐

  1. 浅谈MVP

    MVP是什么 MVP:Model-View-PresenterModel:表示数据提供者:View:表示数据展示:Presenter:是M与V沟通的桥梁. MVP工作方式 UI:告知Presenter ...

  2. python开发初识(一)

    python开发 机器码和字节码 机器码 :计算机可以直接认识的语言 字节码 :高级语言转换成机器码去执行 语言之间的对比: C,汇编 :C语言是根语言 python Java :既能写前端,又能写后 ...

  3. Hill密码解密过程(Java)

    Hill密码是一种传统的密码体系.加密原理:选择一个二阶可逆整数矩阵A称为密码的加密矩阵,也就是这个加密体系的密钥.加密过程: 明文字母依次逐对分组,例如加密矩阵为二阶矩阵,明文就两个字母一组,如果最 ...

  4. Android Google官方文档解析之——Device Compatibility

    Android is designed to run on many different types of devices, from phones to tablets and television ...

  5. 14、Cookie和Session组件

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  6. JS中的Array之方法(1)

    a=[2,4,5,6,7,90]; [1]. a.toString();  // 返回字符串表示的数组,逗号分隔 "2,4,5,6,7,90" [2]. a.join('||'); ...

  7. 使用 .NET 5 体验大数据和机器学习

    翻译:精致码农-王亮 原文:http://dwz.win/XnM .NET 5 旨在提供统一的运行时和框架,使其在各平台都有统一的运行时行为和开发体验.微软发布了与 .NET 协作的大数据(.NET ...

  8. 主动关闭 time-wait 2msl 处理

    先上传后面整理 /* * This routine is called by the ICMP module when it gets some * sort of error condition. ...

  9. Scrum转型(一) 为什么敏捷和Scrum

    1.1 为什么敏捷 由于传统的瀑布模型管理方法无法满足现代某些软件产品开发过程的特点,我们需要使用敏捷的方法(例如,Scrum是一个让我们关注于在短时间里交付高质量商业价值的敏捷框架). 需求频繁变动 ...

  10. ceph-fuse卡顿无法写入的问题

    问题 ceph fuse closing stale session while still operable (Oliver Dzombic) 问题原文: Hi, i am testing on c ...