quatz调度-手动终止线程(1) 创建触发器,线程执行完毕后添加到cleaner list

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerBean;
import com.tdtech.eplatform.gatekeeper.listener.JobTriggerListener;

public class QuartzCenter {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private QuartzUtil quartzUtil;
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private JobTriggerListener jobTriggerListener;

    public String scheduleJob(Object targetObject, String targetMethod, int repeatCount, long repeatInterval, long startDelay) {
        String jobName = quartzUtil.makeJobName(targetMethod);
        try {
            logger.info("scheduleJob jobname:"+jobName);
            if(targetMethod != null && targetMethod.startsWith("run")){                
scheduler.addTriggerListener(jobTriggerListener);
} MethodInvokingJobDetailFactoryBean jobDetailFactoryBean
= new MethodInvokingJobDetailFactoryBean(); jobDetailFactoryBean.setTargetObject(targetObject); jobDetailFactoryBean.setTargetMethod(targetMethod); jobDetailFactoryBean.setName(jobName); jobDetailFactoryBean.setConcurrent(false); jobDetailFactoryBean.afterPropertiesSet(); SimpleTriggerBean simpleTriggerBean = new SimpleTriggerBean(); simpleTriggerBean.setBeanName(quartzUtil.makeTriggerBeanName(targetMethod)); simpleTriggerBean.setRepeatCount(repeatCount); simpleTriggerBean.setRepeatInterval(repeatInterval); simpleTriggerBean.setStartDelay(startDelay); simpleTriggerBean.afterPropertiesSet(); if(targetMethod!=null && targetMethod.startsWith("run")) { simpleTriggerBean.addTriggerListener("conTriggerListener"); } scheduler.scheduleJob((JobDetail) jobDetailFactoryBean.getObject(), simpleTriggerBean); } catch (Exception e) { jobName = null; logger.error("scheduleJob failed!", e); } return jobName; } public boolean terminateJob(String jobName) { if (jobName == null) { return false; } try { return scheduler.deleteJob(jobName, null); } catch (Exception e) { logger.error("terminate job failed![jobName] is " + jobName, e); } return false; } }

 

JobTriggerListener 触发器

import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;
public class JobTriggerListener implements TriggerListener {

    @Override
    public String getName() {
        return "conTriggerListener";
    }

    @Override
    public void triggerComplete(Trigger arg0, JobExecutionContext arg1, int arg2) {

        Date d = arg1.getNextFireTime();
        String jobName  = arg1.getJobDetail().getName();
        if (d == null && jobName!=null && jobName.startsWith("run")) {
            Cleaner.getInstance().put(arg1.getJobDetail().getName());
        }
    }

    @Override
    public void triggerFired(Trigger arg0, JobExecutionContext arg1) {
    }

    @Override
    public void triggerMisfired(Trigger arg0) {
    }

    @Override
    public boolean vetoJobExecution(Trigger arg0, JobExecutionContext arg1) {
        return false;
    }

}

 

//生成线程名 uuid

import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.NoArgGenerator;


public class QuartzUtil {
    /**
     * 使用UUID避免生成重复的名称
     * 影响程序性能,毕竟字符串比较非常耗时
     */
    private final NoArgGenerator uuidGenerator = Generators.randomBasedGenerator();

    public String makeJobName(String customName) {
        return customName + "_" + uuidGenerator.generate().toString();
    }

    public String makeTriggerBeanName(String customName) {
        return customName + "_" + uuidGenerator.generate().toString();
    }

}

 

//业务流程

    protected void disconnectChannel() {

           ...
            String jobName = quartzCenter.scheduleJob(new CheckShouldCloseChannel(userDataIndex), "run", CHECK_CHANNEL_SHOULD_CLOSE_CNT - 1, 10000L, 10000L);
           ...
    }    

 

posted @ 2017-05-23 20:51  eaglediao  阅读(325)  评论(0编辑  收藏  举报