当前位置:网站首页>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
边栏推荐
- Definition and precautions of genuine St link/v2 j-link jtag/swd pin
- 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
- 城市大脑知识图谱构建及应用研究
- 河狸生存记:90后女博士与AI开发者们
- Visual studio 2022 17.1 is now available!
- Appium automated test Jiugongge unlock
- Usage example of qjsonobject
- Building web automation environment
- 犹豫的根音
- [width first search note] BFS output shortest path
猜你喜欢

Un voyage profond d'IA dans Huawei Cloud

Use of axurer9 master

User network model and QoE

Construction and application of urban brain knowledge map

Use of axurer9 option group

项目管理到底管的是什么?

Icon fill color and background color change together

Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices

CVPR 2022|极具创意&美感的文字生成方法!支持任意输入

面试官:Redis中字符串的内部实现方式是什么?
随机推荐
How to open a safe and reliable securities account in the financial management class of qiniu school?
Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox
Appium automated test Jiugongge unlock
用指针计算数组的个数
开通股票炒股账号安全吗?是靠谱的吗?
[dynamic programming] p1018 linear DP: maximum product
Microsoft's exclusive payment function has also been perfectly unlocked
阿海的忠告
If you are a C developer, look at these three explicit programming techniques
Definition and precautions of genuine St link/v2 j-link jtag/swd pin
E-commerce is popular, how to improve the store conversion rate?
别再问我,UI自动化测试怎么做了……
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
Interface use case design
华为云的AI深潜之旅
Explanation and usage of sqrt() function
Security dilemma of NFT liquidity agreement - Analysis of the hacked event of NFT loan agreement xcarnival
apipost脚本使用讲解一~全局变量
How to analyze the relationship between enterprise digital transformation and data asset management?
How do independent site sellers efficiently manage complex Facebook pages?