当前位置:网站首页>Quartz定时任务触发器启动时设置
Quartz定时任务触发器启动时设置
2022-06-28 21:47:00 【Yuyy】
本文最后更新于 757 天前,其中的信息可能已经有所发展或是发生改变。
需求
- 为了将触发器的Cron表达式放在数据库里,方便维护。所以需要在项目启动时去数据库获取Cron表达式,并配置触发器,添加到调度器里。
实现
- 在配置定时任务相关的Bean时,通过设置
init方法,实现配置Bean时执行从数据库获取Cron表达式。 - 因为需要新增
ini方法,所以需要新建一个类,继承CronTriggerBean。 - 在
ini方法中从数据库获取Cron表达式,并设置到trigger里。
<bean id="Task" class="xxx" >
</bean>
<bean id="TaskDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="Task" />
</property>
<property name="targetMethod">
<value>exec</value>
</property>
</bean>
<!--触发器的Cron表达式是启动时从数据库获取的 -->
<bean id="TaskTrigger" class="CronTriggerBean"
lazy-init="false" init-method="init">
<property name="jobDetail" ref="TaskDetail" />
</bean>public class CronTriggerBean extends CronTriggerBean{
public void init(){
String hql = "xxx";
List<TRetryCron> list = Dao.getHibernateTemplate().find(sb.toString());
String cronExpression=null;
if(null!=list&&list.size()>0){
cronExpression=list.get(0).getCCronExpression();
}else{
// 避免没能从数据库取到Cron表达式,导致出现异常,这儿给个永远不会执行的Cron表达式
cronExpression="0 0 5 31 2 ?";
logger.error("未能从数据库取到有效的Cron表达式。");
}
try {
super.setCronExpression(cronExpression);
} catch (ParseException e) {
logger.error(String.format("解析Cron表达式失败,设置一个永不执行的Cron表达式【0 0 5 31 2 ?】,解析失败的Cron表达式为【%s】。", cronExpression));
// 解析Crontab表达式失败,设置一个永不执行的Cron表达式
try {
super.setCronExpression("0 0 5 31 2 ?");
} catch (ParseException e1) {
}
}
}
}Post Views: 312
边栏推荐
- Construction and application of urban brain knowledge map
- 别再问我,UI自动化测试怎么做了……
- 零基础自学SQL课程 | SQL中的日期函数大全
- 视觉弱监督学习研究进展
- Progress in visual weakly supervised learning
- Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox
- Lumiprobe protein labeling research scheme
- PHP uses stack to solve maze problem
- ansible生产环境使用场景(七):批量部署elk客户端
- E-commerce is popular, how to improve the store conversion rate?
猜你喜欢

Interface use case design

6年心得,从功能测试到测试开发,送给在测试路上一路走到黑的你

E-commerce is popular, how to improve the store conversion rate?
![Sword finger offer:[day 2 linked list (simple)] --- > reverse linked list](/img/05/0d7ed95f83c2188d274dde84817dc3.jpg)
Sword finger offer:[day 2 linked list (simple)] --- > reverse linked list

Safety innovation practice | Haitai Fangyuan was invited to participate in the technical exchange Seminar on "network information innovation and value co creation in the digital age"

Postman introduction and installation steps

面试官:Redis中字符串的内部实现方式是什么?

Zero foundation self-study SQL course | complete collection of date functions in SQL

ROS 2 Humble Hawksbill 之 f1tenth gym

How to analyze the relationship between enterprise digital transformation and data asset management?
随机推荐
Akamai acquires linode
科技巨头成立元宇宙标准论坛,走向开放还是建立围城?
Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
Ctrip will push the "3+2" working mode: 3 days a week on duty and 2 days of free choice of office space. Do you envy it?
为什么要使用 Rust 语言?
IPv6 comprehensive experiment
6月底了,让我康康有多少准备跳槽的
零基础自学SQL课程 | SQL中的日期函数大全
In one sentence, I will tell you the meaning of select 1, 2 and 3 in SQL injection, and explain the meaning of each part of SQL injection in detail
Laravel文档阅读笔记-Adding a Markdown editor to Laravel
Lua source code analysis: 1 Lua variable type mutability is implemented in C code.
10、标准I/O输入输出重定向及管道
Introduction to wrk pressure test tool
视觉弱监督学习研究进展
Interface use case design
QJsonObject的使用示例
Biovendor free light chain( κ and λ) Test steps of ELISA Kit
Lumiprobe protein labeling research scheme
职业问诊 | 在数据分析面试中,这样做自我介绍才靠谱
Construction and application of urban brain knowledge map