场景,需要动态生成定时器,并且把一些参数存在定时器中,在日终执行时的方法里带上参数。
1:动态日终生成,销毁方法。
package com.focustech.ins.web.financial.service; import com.focustech.ins.financial.model.FinancialActiveComm; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.CronTriggerBean; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Created by qiyanzhou on 2016/10/9. */ @Service public class CronScheduler { @Autowired FinancialClockQuartz financialClockQuartz; @Resource(name = "startQuartz") private Scheduler scheduler; private final static Log logger = LogFactory.getLog(CronScheduler.class); public void createQuartz(FinancialActiveComm financialActiveComm) { try { // create JOB JobDetail jobDetail = new JobDetail(); jobDetail.setJobClass(financialClockQuartz.getClass()); jobDetail.setName("commId" + financialActiveComm.getCommId().toString()); CronTriggerBean cronTrigger = new CronTriggerBean(); cronTrigger.setBeanName("commId" + financialActiveComm.getCommId().toString()); cronTrigger.setCronExpression(financialActiveComm.getDayEndCron()); // 测试用 // cronTrigger.setCronExpression("00 20 16 17 10 ? 2016"); // cronTrigger.setCronExpression("*/15 * * * * ?"); cronTrigger.afterPropertiesSet(); jobDetail.getJobDataMap().put("commId", financialActiveComm.getCommId()); scheduler.scheduleJob(jobDetail, cronTrigger); logger.info("激励佣金创建日终,佣金ID:" + financialActiveComm.getCommId() + "CRON表达式" + financialActiveComm.getDayEndCron()); // Start Scheduler scheduler.start(); } catch (Exception e) { e.printStackTrace(); } } public void deleteCronScheduler(FinancialActiveComm financialActiveComm) { try { scheduler.unscheduleJob("commId" + financialActiveComm.getCommId().toString(), Scheduler.DEFAULT_GROUP); logger.info("激励佣金删除日终,佣金ID:" + financialActiveComm.getCommId() + "CRON表达式" + financialActiveComm.getDayEndCron()); } catch (Exception e) { e.printStackTrace(); } } }
2:能获取定时器参数的执行类(继承QuartzJobBean 的executeInternal)
package com.focustech.ins.web.financial.service; import cn.xyz.io.alch.dto.FinancialUserInfoDTO; import cn.xyz.io.alch.dto.comm.ActiveFinancialApiDtoAdapter; import cn.xyz.io.alch.dto.comm.EncourCommFormDTO; import cn.xyz.io.alch.encoursettle.api.EncourSettleSheetProxyProvider; import com.focustech.ins.common.utils.SpringContextUtils; import com.focustech.ins.financial.FinancialCalculateRuleProvider; import com.focustech.ins.financial.dao.FinancialActiveCommDAO; import com.focustech.ins.financial.dto.avtiveFinancial.ActiveFinancialApiDto; import com.focustech.ins.web.batch.mail.sms.service.SendBatchMailSheudle; import com.focustech.ins.web.financial.api.FinancialCalculateRuleProviderImpl; import com.focustech.ins.web.orderremind.service.WapAndAppOrderRemindService; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Service; /** * Created by qiyanzhou on 2016/10/9. */ @Service public class FinancialClockQuartz extends QuartzJobBean { private static final Log logger = LogFactory.getLog(CronScheduler.class); public void executeInternal(JobExecutionContext context) throws JobExecutionException { logger.info("激励佣金结算触发,id为"+(Long)context.getMergedJobDataMap().get("commId")); EncourSettleSheetProxyProvider encourSettleSheetProxyProvider = null; try { encourSettleSheetProxyProvider = SpringContextUtils.getBean("encourSettleSheetProxyProvider"); } catch(Exception e){ logger.info("获得实例时报错"+e); } FinancialUserInfoDTO user = null; try { ActiveFinancialApiDtoAdapter adapter = change2EncourCommFormDTO((Long)context.getMergedJobDataMap().get("commId")); EncourCommFormDTO encourForm = adapter.getEncourForm(); String startTime = adapter.getSettleStartTime(); String endTime = adapter.getSettleEndTime(); Long res = encourSettleSheetProxyProvider.createEncourSettleSheet(startTime, endTime, encourForm, user); logger.debug("FinancialClockQuartz.Return:" + res); } catch (Exception e) { e.printStackTrace(); logger.info("向财务系统服务请求时报错"+e); } } }