数据库脚本文件,数据库使用的是sqlservice,别的数据库的,脚本可以自行修改。
/*
Navicat Premium Data Transfer
Source Server : 192.168.2.79-xjj
Source Server Type : SQL Server
Source Server Version : 10501600
Source Host : 192.168.2.79:1433
Source Catalog : INTRUST
Source Schema : dbo
Target Server Type : SQL Server
Target Server Version : 10501600
File Encoding : 65001
Date: 06/06/2020 10:32:55
*/
-- ----------------------------
-- Table structure for TIME_TASK
-- ----------------------------
IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[TIME_TASK]') AND type IN ('U'))
DROP TABLE [dbo].[TIME_TASK]
GO
CREATE TABLE [dbo].[TIME_TASK] (
[SERIAL_NO] int NOT NULL,
[CRON] varchar(16) COLLATE Chinese_PRC_CI_AS NULL,
[CRON_TYPE] int NULL
)
GO
ALTER TABLE [dbo].[TIME_TASK] SET (LOCK_ESCALATION = TABLE)
GO
EXEC sp_addextendedproperty
'MS_Description', N'cron表达式',
'SCHEMA', N'dbo',
'TABLE', N'TIME_TASK',
'COLUMN', N'CRON'
GO
EXEC sp_addextendedproperty
'MS_Description', N'类型',
'SCHEMA', N'dbo',
'TABLE', N'TIME_TASK',
'COLUMN', N'CRON_TYPE'
GO
-- ----------------------------
-- Records of TIME_TASK
-- ----------------------------
INSERT INTO [dbo].[TIME_TASK] VALUES (N'1', N'0 */5 * * * ?', N'4001')
GO
INSERT INTO [dbo].[TIME_TASK] VALUES (N'2', N'0 */30 * * * ?', N'4002')
GO
INSERT INTO [dbo].[TIME_TASK] VALUES (N'3', N'0 */10 * * * ?', N'4003')
GO
-- ----------------------------
-- Primary Key structure for table TIME_TASK
-- ----------------------------
ALTER TABLE [dbo].[TIME_TASK] ADD CONSTRAINT [PK__TIME_TAS__2A5625AA4D82A22F] PRIMARY KEY CLUSTERED ([SERIAL_NO])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [SDATA]
GO
实现代码
package com.xjj.modules.timetask.config;
import com.alibaba.druid.util.StringUtils;
import com.xjj.modules.timetask.mapper.CronMapper;
import com.xjj.modules.timetask.service.TimeTaskService;
import lombok.extern.slf4j.Slf4j;
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.stereotype.Component;
/**
* Created by wumingkai on 2020-6-5.
*/
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
@Slf4j
public class TimeTaskSchedule implements SchedulingConfigurer {
@Autowired
private CronMapper cronMapper;
/* @Autowired
private TimeTaskService timeTaskService;*/
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
//scheduledTaskRegistrar.addCronTask( () -> timeTaskService.reemAndSg(),cronMapper.getCron());
scheduledTaskRegistrar.addTriggerTask(
//1.添加任务内容(Runnable)
() -> System.out.println("定时任务执行"),
//2.设置执行周期(Trigger)
triggerContext -> {
//2.1 从数据库获取执行周期
String cron = cronMapper.getCron();
//2.2 合法性校验.
if (StringUtils.isEmpty(cron)) {
// Omitted Code ..
log.warn("表达式为空");
}
//2.3 返回执行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}
}
package com.xjj.modules.timetask.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* Created by wumingkai on 2020-6-5.
*/
@Mapper
public interface CronMapper {
@Select("SELECT CRON FROM TIME_TASK WHERE CRON_TYPE = '4001'")
String getCron();
}
这里会去查询你数据库里面设置的cron表达式,去赋值给定时任务。