34. springboot动态配置定时任务(通过数据库配置)

1、pom.xml文件


	4.0.0

	test.task
	taskTest
	0.0.1-SNAPSHOT
	jar

	taskTest
	http://maven.apache.org

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.6.RELEASE
		 
	

	
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			com.alibaba
			fastjson
			1.2.3
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
		
			mysql
			mysql-connector-java
		
		
			com.alibaba
			druid
			1.1.2
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

2、启动类App.java

package task.test;

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.scheduling.annotation.EnableScheduling;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableAutoConfiguration
@EnableScheduling
@MapperScan({"task.test.mapper"})
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

3、实体类Cron

package task.test.entity;

public class Cron {
    private String cronId;

    private String cron;

    public String getCronId() {
        return cronId;
    }

    public void setCronId(String cronId) {
        this.cronId = cronId == null ? null : cronId.trim();
    }

    public String getCron() {
        return cron;
    }

    public void setCron(String cron) {
        this.cron = cron == null ? null : cron.trim();
    }
}

4、CronMapper.java

package task.test.mapper;

import java.util.List;

import task.test.entity.Cron;

public interface CronMapper {
    int insert(Cron record);

    List selectAll();
}

5、CronMapper.xml




  
    
    
  
  
    insert into cron (cron_id, cron)
    values (#{cronId,jdbcType=VARCHAR}, #{cron,jdbcType=VARCHAR})
  
  

6、任务调度配置类ScheduleConfig

package task.test.config;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.StringUtils;

import task.test.mapper.CronMapper;

@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {

	@Autowired
	private CronMapper cronMapper;

	/**
	 * 执行定时任务.
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.addTriggerTask(
				// 1.添加任务内容(Runnable)
				() -> {
					// 1.1具体任务
					System.out.println("执行定时任务: " + LocalDateTime.now().toLocalTime());
				},
				// 2.设置执行周期(Trigger)
				triggerContext -> {
					// 2.1 从数据库获取执行周期
					String cron = cronMapper.selectAll().get(0).getCron();
					// 2.2 合法性校验.
					if (StringUtils.isEmpty(cron)) {
						// 根据需求修改
					}
					// 2.3 返回执行周期(Date)
					return new CronTrigger(cron).nextExecutionTime(triggerContext);
				});
	}
}

7、application.yml配置文件

spring:
  datasource:
    url: jdbc:mysql://***:3306/***
    username: root
    password: root

8、数据库建表语句

DROP TABLE IF EXISTS `cron`;
CREATE TABLE `cron` (
 `cron_id` varchar(30),
 `cron` varchar(30) 
);
INSERT INTO `cron` VALUES ('1', '0/5 * * * * ?');

 

你可能感兴趣的:(Spring,boot学习笔记)