Quartz动态添加定时任务执行sql(服务启动添加+手动添加)

这篇博客介绍了如何在Java应用中使用Quartz动态添加定时任务,包括服务启动时自动添加和手动添加。详细讲解了数据库表设计、系统定时任务类、计划任务执行类、工具类的实现,并提供了配置文件的示例。内容涵盖接口配置、Cron表达式的使用以及手动添加任务的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统用来每天插入视图数据。。。

一、数据库表设计

1、接口配置表(t_m_db_interface_config

 

2、接口日志表(t_m_db_interface_log

 

3、前端配置页面

查询页面:

新增及修改页面:

第一个sql一般用来删除原有数据,第二个sql一般用来插入新数据,多条sql可以写在一起,代码中做了批处理,用分号隔开(英文的分号)。

不配置临界时间点时sql示例:delete from table_ where BUSSINESS_DATE>=DATE_FORMAT(NOW(),'%Y-%m-%d');

配置临界时间点sql示例:delete from table_ where BUSSINESS_DATE>=DATE_FORMAT(?,'%Y-%m-%d');

时间条件可自己修改,注意的是如果sql中有?号则说明配置了临界时间点,代码中会根据设置的临界时间点来确定是今天还是昨天。

4、单表类

上述两张表的字段设计可根据实际需求灵活调整。

按mvc的开发模式,写出上述两个表对应的对码。也可以Mybatis工具自动分包生成。

分别位于com.dbs.dmsmdm.(controller/service/mapper/bean).simple路径下。

xml映射文件位于resources/mybatis+同上路径下。

这里只贴出bean层代码,Controllor、Service、dao、mapper就不贴出来了。

前面的@ApiModelProperty注解为自定义的注解,请忽略。。。

  1 package com.dbs.dmsmdm.bean.simple;
  2 
  3 import com.dbs.dms.uibase.bean.BaseBean;
  4 import java.util.Date;
  5 
  6 import org.springframework.format.annotation.DateTimeFormat;
  7 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  8 import com.fasterxml.jackson.annotation.JsonFormat;
  9 import com.dbs.dms.uibase.annotations.ApiModel;
 10 import com.dbs.dms.uibase.annotations.ApiModelProperty;
 11 import com.dbs.dms.translate.TranslateBean;
 12 import com.dbs.dms.translate.TranslateBeanSerializer;
 13 /**
 14  * 接口配置表
 15  * 实体类对应的数据表为:  t_m_db_interface_config
 16  * @author $Author
 17  * @date Tue Apr 17 11:21:18 GMT+08:00 2018
 18  */
 19 @ApiModel(value ="DbInterfaceConfigBean")
 20 public class DbInterfaceConfigBean extends BaseBean {
 21     public static final String ATTR_INTERFACE_CONFIG_ID = "interfaceConfigId";
 22     public static final String ATTR_INTERFACE_CODE = "interfaceCode";
 23     public static final String ATTR_INTERFACE_NAME = "interfaceName";
 24     public static final String ATTR_FROM_SYSTEM = "fromSystem";
 25     public static final String ATTR_TO_SYSTEM = "toSystem";
 26     public static final String ATTR_FREQENCY = "freqency";
 27     public static final String ATTR_FREQENCY_TYPE = "freqencyType";
 28     public static final String ATTR_BEGIN_RUN_TIME = "beginRunTime";
 29     public static final String ATTR_NEXT_RUN_TIME = "nextRunTime";
 30     public static final String ATTR_TIME_RUN_YESTERDAY = "timeRunYesterday";
 31     public static final String ATTR_BEFORE_SQL = "beforeSql";
 32     public static final String ATTR_RUN_SQL = "runSql";
 33     public static final String ATTR_AFTER_SQL = "afterSql";
 34     
 35     @ApiModelProperty(value = "INTERFACE_CONFIG_ID",label = "接口配置",dataType="varchar(36)",length="36",primary=true,required=true)
 36     private String interfaceConfigId;
 37 
 38     @ApiModelProperty(value = "INTERFACE_CODE",label = "接口编码",dataType="varchar(50)",length="50")
 39     private String interfaceCode;
 40 
 41     @ApiModelProperty(value = "INTERFACE_NAME",label = "接口名称",dataType="varchar(200)",length="200")
 42     private String interfaceName;
 43 
 44     @ApiModelProperty(value = "FROM_SYSTEM",label = "源系统:DB0081",dataType="varchar(20)",length="20")
 45     private String fromSystem;
 46 
 47     @ApiModelProperty(value = "TO_SYSTEM",label = "目标系统:DB0081",dataType="varchar(20)",length="20")
 48     private String toSystem;
 49 
 50     @ApiModelProperty(value = "FREQENCY",label = "接口频率",dataType="integer",length="0")
 51     private Integer freqency;
 52 
 53     @ApiModelProperty(value = "FREQENCY_TYPE",label = "频率类别:DB0082",dataType="varchar(2)",length="2")
 54     private String freqencyType;
 55 
 56     @ApiModelProperty(value = "BEGIN_RUN_TIME",label = "开始运行时间",dataType="datetime",length="0")
 57     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 58     private Date beginRunTime;
 59 
 60     @ApiModelProperty(value = "NEXT_RUN_TIME",label = "下次运行时间",dataType="datetime",length="0")
 61     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 62     private Date nextRunTime;
 63 
 64     @ApiModelProperty(value = "TIME_RUN_YESTERDAY",label = "N点之前执行前一天数据",dataType="integer",length="0")
 65     private Integer timeRunYesterday;
 66 
 67     @ApiModelProperty(value = "CREATOR",label = "创建人",dataType="varchar(50)",length="50",comment="创建人")
 68     private String creator;
 69 
 70     @ApiModelProperty(value = "CREATED_DATE",label = "创建时间",dataType="datetime",length="0",comment="创建时间")
 71     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 72     private Date createdDate;
 73 
 74     @ApiModelProperty(value = "MODIFIER",label = "最后更新人员",dataType="varchar(50)",length="50",comment="最后更新人员")
 75     private String modifier;
 76 
 77     @ApiModelProperty(value = "LAST_UPDATED_DATE",label = "最后更新时间",dataType="timestamp",length="0",comment="最后更新时间")
 78     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 79     private Date lastUpdatedDate;
 80 
 81     @ApiModelProperty(value = "IS_ENABLE",label = "是否可用",dataType="varchar(2)",length="2",comment="是否可用 1启用,0禁用,2删除")
 82     private String isEnable;
 83 
 84     @ApiModelProperty(value = "UPDATE_CONTROL_ID",label = "并发控制字段",dataType="varchar(36)",length="36",comment="并发控制字段")
 85     private String updateControlId;
 86 
 87     public String getInterfaceConfigId() {
 88         return interfaceConfigId;
 89     }
 90 
 91     public void setInterfaceConfigId(String interfaceConfigId) {
 92         this.interfaceConfigId = interfaceConfigId;
 93     }
 94 
 95     public String getInterfaceCode() {
 96         return interfaceCode;
 97     }
 98 
 99     public void setInterfaceCode(String interfaceCode) {
100         this.interfaceCode = interfaceCode;
101     }
102 
103     public String getInterfaceName() {
104         return interfaceName;
105     }
106 
107     public void setInterfaceName(String interfaceName) {
108         this.interfaceName = interfaceName;
109     }
110 
111     public String getFromSystem() {
112         return fromSystem;
113     }
114 
115     public void setFromSystem(String fromSystem) {
116         this.fromSystem = fromSystem;
117     }
118 
119     public String getToSystem() {
120         return toSystem;
121     }
122 
123     public void setToSystem(String toSystem) {
124         this.toSystem = toSystem;
125     }
126 
127     public Integer getFreqency() {
128         return freqency;
129     }
130 
131     public void setFreqency(Integer freqency) {
132         this.freqency = freqency;
133     }
134 
135     public String getFreqencyType() {
136         return freqencyType;
137     }
138 
139     public void setFreqencyType(String freqencyType) {
140         this.freqencyType = freqencyType;
141     }
142 
143     @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
144     public Date getBeginRunTime() {
145         return beginRunTime;
146     }
147 
148     public void setBeginRunTime(Date beginRunTime) {
149         this.beginRunTime = beginRunTime;
150     }
151 
152     @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
153     public Date getNextRunTime() {
154         return nextRunTime;
155     }
156 
157     public void setNextRunTime(Date nextRunTime) {
158         this.nextRunTime = nextRunTime;
159     }
160 
161     public Integer getTimeRunYesterday() {
162         return timeRunYesterday;
163     }
164 
165     public void setTimeRunYesterday(Integer timeRunYesterday) {
166         this.timeRunYesterday = timeRunYesterday;
167     }
168 
169     public String getCreator() {
170         return creator;
171     }
172 
173     public void setCreator(String creator) {
174         this.creator = creator;
175     }
176 
177     @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
178     public Date getCreatedDate() {
179         return createdDate;
180     }
181 
182     public void setCreatedDate(Date createdDate) {
183         this.createdDate = createdDate;
184     }
185 
186     public String getModifier() {
187         return modifier;
188     }
189 
190     public void setModifier(String modifier) {
191         this.modifier = modifier;
192     }
193 
194     @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
195     public Date getLastUpdatedDate() {
196         return lastUpdatedDate;
197     }
198 
199     public void setLastUpdatedDate(Date lastUpdatedDate) {
200         this.lastUpdatedDate = lastUpdatedDate;
201     }
202 
203     public String getIsEnable() {
204         return isEnable;
205     }
206 
207     public void setIsEnable(String isEnable) {
208         this.isEnable = isEnable;
209     }
210 
211     public String getUpdateControlId() {
212         return updateControlId;
213     }
214 
215     public void setUpdateControlId(String updateControlId) {
216         this.updateControlId = updateControlId;
217     }
218 }
DbInterfaceConfigBean
  1 package com.dbs.dmsmdm.bean.simple;
  2 
  3 import com.dbs.dms.uibase.bean.BaseBean;
  4 import java.util.Date;
  5 
  6 import org.springframework.format.annotation.DateTimeFormat;
  7 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
  8 import com.fasterxml.jackson.annotation.JsonFormat;
  9 import com.dbs.dms.uibase.annotations.ApiModel;
 10 import com.dbs.dms.uibase.annotations.ApiModelProperty;
 11 import com.dbs.dms.translate.TranslateBean;
 12 import com.dbs.dms.translate.TranslateBeanSerializer;
 13 /**
 14  * 接口运行日志
 15  * 实体类对应的数据表为:  t_m_db_interface_log
 16  * @author $Author
 17  * @date Tue Apr 17 11:21:18 GMT+08:00 2018
 18  */
 19 @ApiModel(value ="DbInterfaceLogBean")
 20 public class DbInterfaceLogBean extends BaseBean {
 21     public static final String ATTR_IFTERFACE_LOG_ID = "ifterfaceLogId";
 22     public static final String ATTR_INTERFACE_CODE = "interfaceCode";
 23     public static final String ATTR_INTERFACE_NAME = "interfaceName";
 24     public static final String ATTR_FROM_SYSTEM = "fromSystem";
 25     public static final String ATTR_TO_SYSTEM = "toSystem";
 26     public static final String ATTR_ERROR_TIME = "errorTime";
 27     public static final String ATTR_ERROR_MSG = "errorMsg";
 28     
 29     @ApiModelProperty(value = "IFTERFACE_LOG_ID",label = "IFTERFACE_LOG_ID",dataType="varchar(36)",length="36",primary=true,required=true,comment="ID")
 30     private String ifterfaceLogId;
 31 
 32     @ApiModelProperty(value = "INTERFACE_CODE",label = "接口编码",dataType="varchar(50)",length="50",comment="接口编码")
 33     private String interfaceCode;
 34 
 35     @ApiModelProperty(value = "INTERFACE_NAME",label = "接口名称",dataType="varchar(200)",length="200",comment="接口名称")
 36     private String interfaceName;
 37 
 38     @ApiModelProperty(value = "FROM_SYSTEM",label = "源系统",dataType="varchar(20)",length="20",comment="源系统:DB0081")
 39     private String fromSystem;
 40 
 41     @ApiModelProperty(value = "TO_SYSTEM",label = "目标系统",dataType="varchar(20)",length="20",comment="目标系统:DB0081")
 42     private String toSystem;
 43 
 44     @ApiModelProperty(value = "ERROR_TIME",label = "错误时间",dataType="datetime",length="0",comment="错误时间")
 45     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 46     private Date errorTime;
 47 
 48     @ApiModelProperty(value = "CREATOR",label = "创建人",dataType="varchar(50)",length="50",comment="创建人")
 49     private String creator;
 50 
 51     @ApiModel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值