创建动态定时器(jdk1.6下)

场景,需要动态生成定时器,并且把一些参数存在定时器中,在日终执行时的方法里带上参数。

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);
      }
   }




}

转载于:https://my.oschina.net/u/3197054/blog/817882

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值