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 }