定时quart 复制mysql_Linux java mysql 定时备份和手动备份 (二) quartz定时器

该博客主要介绍了一个Spring监听器`DBBackupListener`的实现,它在Spring启动后查询数据库,根据数据库中的信息设置Cron表达式来执行定时任务。监听器通过`ApplicationListener`接口监听`ContextRefreshedEvent`事件,根据数据库中的`AutoBackupMapper`查询到的自动备份信息,更新CronTrigger的定时表达式,从而实现数据库备份的自动化调度。

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

1 packagecn.goldencis.tsa.system.utils;2

3 importjavax.annotation.Resource;4

5 importorg.quartz.CronTrigger;6 importorg.quartz.impl.StdScheduler;7 importorg.springframework.beans.factory.annotation.Autowired;8 importorg.springframework.context.ApplicationListener;9 importorg.springframework.context.event.ContextRefreshedEvent;10 importorg.springframework.stereotype.Component;11 importorg.springframework.web.context.ContextLoader;12

13 importcn.goldencis.tsa.common.utils.CronExpressionUtil;14 importcn.goldencis.tsa.common.utils.QuartzHandler;15 importcn.goldencis.tsa.system.dao.AutoBackupMapper;16 importcn.goldencis.tsa.system.entity.DBBackupMsg;17 importcn.goldencis.tsa.system.entity.QuartzBean;18

19 /**

20 * 监听器,负责spring启动后查询数据库,根据数据库信息设定corn21 *@authormll22 * 2017年4月15日20:35:2023 */

24

25 @Component26 public class DBBackupListener implements ApplicationListener{27

28 final String IS_AUTO_FLAG = "1";29

30 private static DBBackupListener dBBackupListener=newDBBackupListener();31

32 @Autowired33 privateAutoBackupMapper autoBackupMapper;34

35 @Resource(name="cronTrigger")36 privateCronTrigger cronTrigger;37

38 @Resource(name = "quartzfactory")39 privateStdScheduler sts;40

41 @Resource(name = "quartzBean")42 privateQuartzBean quartzBean;43

44 privateDBBackupListener() {45 //TODO Auto-generated constructor stub

46 }47

48 public staticDBBackupListener getInstance(){49 returndBBackupListener;50 }51

52 @Override53 public voidonApplicationEvent(ContextRefreshedEvent event) {54 if(event.getApplicationContext().getParent()==null){55 this.updateCornEx(null);56 }57 }58

59

60 public voidupdateCornEx(DBBackupMsg db) {61 DBBackupMsg mdb=db;62 if(mdb==null){63 mdb =autoBackupMapper.selectAutoBackInfo();64 }else{65 cronTrigger = (CronTrigger)ContextLoader.getCurrentWebApplicationContext().getBean("cronTrigger");66 sts = (StdScheduler)ContextLoader.getCurrentWebApplicationContext().getBean("quartzfactory");67 quartzBean = (QuartzBean)ContextLoader.getCurrentWebApplicationContext().getBean("quartzBean");68 }69

70 String cronEx = null;71 if(IS_AUTO_FLAG.equals(mdb.getDay()) &&IS_AUTO_FLAG.equals(mdb.getWeek())){72 cronEx =CronExpressionUtil.getCronExByDate(mdb.getDayParam(), mdb.getWeekParam());73 }else if(IS_AUTO_FLAG.equals(mdb.getWeek())){74 cronEx =CronExpressionUtil.getCronExByWeek( mdb.getWeekParam());75 }else if(IS_AUTO_FLAG.equals(mdb.getDay())){76 cronEx =CronExpressionUtil.getCronExByDay( mdb.getDayParam());77 }78 //cronEx="*/5 * * * * ?";

79 QuartzHandler.getInstance().updateCron(cronTrigger, sts, quartzBean.getCronTriggerStr(), quartzBean.getExampleJobStr(), cronEx);80 }81 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值