程序入口类中加入注解

@EnableScheduling

配置定时任务为并行

@Slf4j
@Configuration
public class ScheduledConfig implements SchedulingConfigurer { @Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
}
}//end

编写定时器类

@Component
@Slf4j
public class timerController {
private int fixedDelayCount = 1;
private int fixedDelayCount1 = 1; @Scheduled(fixedDelay = 5000)
public void testFixDelay() throws InterruptedException {
Thread.sleep(5000);
log.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
}
@Scheduled(fixedDelay = 3000)
public void testFixDelay1() {
try {
Thread.sleep(3000);
log.info("===fixedDelay1: 第{}次执行方法", fixedDelayCount1++);
if (fixedDelayCount1 == 3) {
int i = 1 / 0;
}
} catch (Exception e) {
log.error(e.toString());
}
}
}

如果定时器遇到异常,并不会使定时器断掉,抛出异常后会重新运行。

springBoot 定时器的更多相关文章

  1. 补习系列(9)-springboot 定时器,你用对了吗

    目录 简介 一.应用启动任务 二.JDK 自带调度线程池 三.@Scheduled 定制 @Scheduled 线程池 四.@Async 定制 @Async 线程池 小结 简介 大多数的应用程序都离不 ...

  2. springboot 定时器 Schdule

    定时器:定时启动任务,执行代码 1.在启动类中加入注解: 2.创建一个类,并且在这个类上加入注解:@Component 3.定义一个方法,在方法上加入注解:@Scheduled(cron=" ...

  3. SpringBoot定时器任务

    Spring Boot使用@Scheduled定时器任务   摘要: Spring Boot之使用@Scheduled定时器任务 假设我们已经搭建好了一个基于Spring Boot项目,首先我们要在A ...

  4. SpringBoot定时器

    使用Component注解注解一个类,这个类就变成了一个组件.组件可以有很多不同的特性,比如Scheduled注解为组件的某个函数添加了定时的特性. @Component public class M ...

  5. springBoot 定时器任务

    1.新建一个计划任务类(只能和主类平级或在主类的下级) import java.text.SimpleDateFormat; import java.util.Date; import org.slf ...

  6. 关于springboot的定时器配置

    关于springboot的定时器: 比较重要的两个注解: @EnableScheduling:标注启动定时任务. @Scheduled(fixedRate = 1000 * 30)  定义某个定时任务 ...

  7. SpringBoot系列——EnableScheduling,对定时器的支持

    前言 定时器功能在项目里面往往会用到,比如定时发送邮件.定时释放数据库资源:这里记录一下springboot对定时器的支持的简单实例 cron表达式 开始之前要先介绍一下cron表达式,这里当一下百度 ...

  8. Springboot+websocket+定时器实现消息推送

    由于最近有个需求,产品即将到期(不同时间段到期)时给后台用户按角色推送,功能完成之后在此做个小结 1. 在启动类中添加注解@EnableScheduling package com.hsfw.back ...

  9. 关于给springboot添加定时器的两种方式

    原文:https://blog.csdn.net/liboyang71/article/details/72781526 首先,搭建好一个springboot项目,可使用maven或者gradle或者 ...

随机推荐

  1. day23-python之日志 re模块

    1.logging import logging #-----------------------------------logging.basicConfig logging.basicConfig ...

  2. spring boot 设置tomcat post参数限制

    今天传图片,用的base64字符串,POST方法,前端传送的时候总是莫名其妙的崩溃,去网上搜了半天,以为是文件大小被限制了,但是我这个是字符串接收,不是文件接收,于是又继续搜,原来post本身没有参数 ...

  3. HDU:2846-Repository

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...

  4. cogs:1619. [HEOI2012]采花/luogu P2056

    1619. [HEOI2012]采花 ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿是 ...

  5. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  6. maven学习(九)——maven中的坐标、依赖以及仓库

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  7. c++ 中pair类模板的用法详解

    pair: 头文件:#include<utility> 类模板:template <class T1, class T2> struct pair 参数:T1是第一个值的数据类 ...

  8. col-md-push-*和col-md-offset的区别

    实现方式的区别:col-md-offset-*,是利用margin-left实现的,col-md-push-*/col-md-pull-*是利用相对定位实现的. 效果的区别,col-md-offset ...

  9. vue-cli 脚手架分析

    Vue-cli 一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v  检测你是否安装了npm和 ...

  10. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo-client'.

    cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ...