SpringBoot 定时任务的实现

介绍两种实现方式;配置实现和读取数据库定时任务配置实现。

配置实现比较简单。直接撸代码:

package com;

import java.util.Properties;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplicationbr/>@ImportResource("classpath:/config.xml")
@EnableAutoConfiguration
@ComponentScan
@EnableTransactionManagement(proxyTargetClass = true) br/>@MapperScan({"com.*.model.*.mapper","com.*.task"})
@EnableScheduling
public class StartApplication {

public static void main(String[] args) {
    SpringApplication.run(StartApplication.class, args);
}

@Bean
public DatabaseIdProvider getDatabaseIdProvider() {

}

}br/>注:在启动类上加注解
@EnableScheduling
@MapperScan 扫描定时任务配置文件的位置。
其他地方无需关注。

定时任务配置类:
package com.*.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class RunTaskConfig {

private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
//初始延迟1秒,每隔2秒
@Scheduled(fixedRateString = "2000",initialDelay = 1000)
public void testFixedRate(){
    System.out.println("fixedRateString,当前时间:" +format.format(new Date()));
}

//每次执行完延迟2秒
@Scheduled(fixedDelayString= "2000")
public void testFixedDelay(){
    System.out.println("fixedDelayString,当前时间:" +format.format(new Date()));
}
//每隔3秒执行一次
@Scheduled(cron="0/3 * * * * ?")
public void testCron(){
    System.out.println("cron,当前时间:" +format.format(new Date()));
}

}

配置完成,项目启动后配置的定时任务会自动执行。

Springboot 定时任务 的数据库实现方式:
数据表实体
id , 执行类, 执行方法, 是否生效, 时间表达式;

SpringBoot 定时任务实现原理:启动Spring 会扫描@Scheduled 注解,读取注解配置信息。
获取定时任务,解析,注册成可执行的定时任务。

数据库活动定时任务实现具体代码如下
package com.tansun.task;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ScheduledExecutorService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.NamedBeanHolder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.tansun.model.system.dao.RunTaskDao;
import com.tansun.model.system.dao.RunTaskDaoImpl;
import com.tansun.model.system.dao.RunTaskSqlBulider;
import com.tansun.model.system.entity.RunTask;
import com.tansun.web.framework.util.BeanUtil;
/***

SpringBoot 定时任务的实现

原文地址:http://blog.51cto.com/11232071/2135631

时间: 2024-08-30 02:38:22

SpringBoot 定时任务的实现的相关文章

SpringBoot 定时任务的使用

一点知识在JAVA开发领域,目前可以通过以下几种方式进行定时任务: Timer:jdk中自带的一个定时调度类,可以简单的实现按某一频度进行任务执行.提供的功能比较单一,无法实现复杂的调度任务.ScheduledExecutorService:也是jdk自带的一个基于线程池设计的定时任务类.其每个调度任务都会分配到线程池中的一个线程执行,所以其任务是并发执行的,互不影响.Spring Task:Spring提供的一个任务调度工具,支持注解和配置文件形式,支持Cron表达式,使用简单但功能强大.Qu

springBoot定时任务和异步调用

springboot定时任务 在创建好的springboot项目的启动类上加@EnableScheduling注解. @EnableScheduling @SpringBootApplication @MapperScan("com.qianlong.dao") @ComponentScan(value = "com.qianlong") public class DemoApplication { public static void main(String[]

springboot 定时任务@Scheduled注解

需要定时器的地方好像还挺多. 之前项目中有用到使用定时器循环订单时间,然后将超时的订单状态更改. springboot的@Scheduled注解能够很快速完成我们需要的定时任务. @Component public class ExampleTimer { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); /*每100秒执行一次*/ @Scheduled(fixedRate = 100000) public

springboot定时任务,去掉指定日期

今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日. 网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html,整理补充了一下,完成需求. 为了避开节假日,我写触发器,试了下用quartz的Calendar和HolidayCalendar,代码略复杂.放弃. import org.quartz.Calendar;import org.quartz.impl.calendar.HolidayCalendar;

Springboot定时任务原理及如何动态创建定时任务

一.前言 上周工作遇到了一个需求,同步多个省份销号数据,解绑微信粉丝.分省定时将销号数据放到SFTP服务器上,我需要开发定时任务去解析文件.因为是多省份,服务器.文件名规则.数据规则都不一定,所以要做成可配置是有一定难度的.数据规则这块必须强烈要求统一,服务器.文件名规则都可以从配置中心去读.每新增一个省份的配置,后台感知到后,动态生成定时任务. 二.Springboot引入定时任务核心配置 @Target(ElementType.TYPE) @Retention(RetentionPolicy

spring-boot 定时任务需要注意的地方

spring-boot 跑定时任务非常容易 启动类上添加两个注解基本OK @EnableScheduling @EnableAsync 当然要记录的肯定不是这里的问题了 首先, fixedDelayfixedRade 在不开启异步的条件下,这俩的区别还是很大的, fixedDelay : 任务执行完成后等待多长时间执行 fixedRade : 任务下次的执行与本次开始的间隔时间   原文地址:https://www.cnblogs.com/zhaiyt/p/11077631.html

一文搞懂springboot定时任务

Introduction 在springboot中自带了一个轻量级的调度系统.如果我们希望在特定的时间或者以特定的时间间隔完成某些任务,那么它完全能够满足需求,不需要再额外引入像Quartz这种略显沉重的调度框架.下面我们就来介绍springboot中@scheduled 注解的用法. 环境:springboot 2.2.2 常用简单定时任务 首先,为了使用springboot中的定时任务,需要在springboot应用中加入 @EnableScheduling 注解.该注解开启对定时任务的支持

SpringBoot定时任务详解

阅读目录: 序言 一.静态:基于注解 二.动态:基于接口 三.多线程定时任务 阅读正文: 回到顶部 序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库中读取指定时间来动态执行定时任务,这时候基于接口的定时任务就派上用场了. 三.基于注解设定多线程定时任务 回到顶部 一.静态:基于注解 基于注解@Scheduled默认

第二十一章 springboot + 定时任务

1.application.properties #cron job.everysecond.cron=0/1 * * * * * job.everytensecond.cron=0/10 * * * * * job.everyminute.cron=0 0/1 * * * * job.everysecond2.cron=* 0/1 * * * * 注意:cron表达式 第一个:每秒 第二个:每10秒 第三个:每分 第四个:每秒(注意这个不是每分) 2.CronJobTest.java 1 pa