zoukankan      html  css  js  c++  java
  • 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);
               ...
        }    
  • 相关阅读:
    mysql整理-常用sql语句
    WAMP中的mysql设置密码
    cmd中输入net start mysql 提示:服务名无效或者MySQL正在启动 MySQL无法启动
    “laravel.log” could not be opened: failed to open stream
    RESTful API 设计指南
    理解RESTful架构
    PHP:API 接口规范完整版本
    php的api接口
    laravel5.5部署
    《面向对象程序设计》课程作业二
  • 原文地址:https://www.cnblogs.com/eaglediao/p/6896038.html
Copyright © 2011-2022 走看看