当前位置:网站首页>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
边栏推荐
- 加刚干的前提
- 華為雲的AI深潜之旅
- rosdep update 使用小鱼fishros解决ros1/ros2问题 2022
- Hyperjumptech/grule-rule-engine: rule engine implementation of golang
- Webrtc audio and video development - experience
- Dart的类扩展、可选类型扩展
- Biovendor free light chain( κ and λ) Test steps of ELISA Kit
- 零基础自学SQL课程 | SQL中的日期函数大全
- Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
- 开通股票炒股账号安全吗?是靠谱的吗?
猜你喜欢

Laravel文档阅读笔记-Adding a Markdown editor to Laravel

科技巨头成立元宇宙标准论坛,走向开放还是建立围城?

QJsonObject的使用示例

Un voyage profond d'IA dans Huawei Cloud

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

终于有人把云原生架构讲明白了
![Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end](/img/d6/824985b74b27a1bee456c2cebbac26.jpg)
Sword finger offer:[day 2 linked list (simple)] --- > print the linked list from end to end

视觉弱监督学习研究进展

Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox

17 `bs object Node name h3 Parent ` parents get parent node ancestor node
随机推荐
The rogue downloader named by 315 is back
项目管理到底管的是什么?
Competition rules for the "network security" event of the secondary vocational group in the skills competition of Guangxi Vocational Colleges in 2022
ansible生产环境使用场景(七):批量部署elk客户端
There is no picture problem when using tcpdf to generate pdf
PAT 1021. Traversal of the deep root (25 points) graph, DFS, calculating the number of connected components
IPv6 comprehensive experiment
Adding a markdown editor to lavel
CVPR 2022 𞓜 a creative and aesthetic text generation method! Support any input
QStringLiteral(str)
Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol
apipost脚本使用讲解一~全局变量
Pat 1054 the dominiant color (20 points)
Google search is dying | DKB
Construction and application of urban brain knowledge map
Openfire user and group relationship migration
Hashicorp/raft introduction and source code analysis (III): introduction to cluster node recovery
What is an interface? What is interface testing?
Architecture design of e-commerce secsha system
Microsoft's exclusive payment function has also been perfectly unlocked