자바 동적 증가 정시 작업

20981 단어
직접 코드
import org.apache.tools.ant.util.DateUtils;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Calendar;
import java.util.Date;


/**
 * @Description:        
 * Created by zhangxb on 2017/2/10.
 */
public class QuartzManager {
    private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
    private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
    private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";

    /**
     * @Description:         ,         ,    ,     
     *
     * @param jobName
     *               
     * @param cls
     *              
     * @param time
     *                ,  quartz    
     *
     * @Title: QuartzManager.java
     */
    @SuppressWarnings("unchecked")
    public static void addJob(String jobName, Class cls, String time,Object params) {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);//    ,   ,     
            jobDetail.getJobDataMap().put("params", params);
            //    
            CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);//     ,    
            trigger.setCronExpression(time);//        
            sched.scheduleJob(jobDetail, trigger);
            //   
            if (!sched.isShutdown()) {
                sched.start();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:         
     *
     * @param jobName
     *               
     * @param jobGroupName
     *                
     * @param triggerName
     *                
     * @param triggerGroupName
     *                 
     * @param jobClass
     *              
     * @param time
     *                ,  quartz    
     *
     */
    @SuppressWarnings("unchecked")
    public static void addJob(String jobName, String jobGroupName,
                              String triggerName, String triggerGroupName, Class jobClass,
                              String time,Object params) {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);//    ,   ,     
            jobDetail.getJobDataMap().put("params",params);
            //    
            CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);//     ,    
            trigger.setCronExpression(time);//        
            sched.scheduleJob(jobDetail, trigger);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:            (         ,    ,     )
     *
     * @param jobName
     * @param time
     *
     */
    @SuppressWarnings("unchecked")
    public static void modifyJobTime(String jobName, String time) {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
            if (trigger == null) {
                return;
            }
            String oldTime = trigger.getCronExpression();
            if (!oldTime.equalsIgnoreCase(time)) {
                JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME);
                Class objJobClass = jobDetail.getJobClass();
                removeJob(jobName);
                addJob(jobName, objJobClass, time ,jobDetail.getJobDataMap());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:            
     *
     */
    public static void modifyJobTime(String triggerName,
                                     String triggerGroupName, String time) {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName);
            if (trigger == null) {
                return;
            }
            String oldTime = trigger.getCronExpression();
            if (!oldTime.equalsIgnoreCase(time)) {
                CronTrigger ct = (CronTrigger) trigger;
                //     
                ct.setCronExpression(time);
                //      
                sched.resumeTrigger(triggerName, triggerGroupName);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:       (         ,    ,     )
     *
     * @param jobName
     *
     */
    public static void removeJob(String jobName) {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);//      
            sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);//      
            sched.deleteJob(jobName, JOB_GROUP_NAME);//     
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:       
     *
     */
    public static void removeJob(String jobName, String jobGroupName,
                                 String triggerName, String triggerGroupName) {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            sched.pauseTrigger(triggerName, triggerGroupName);//      
            sched.unscheduleJob(triggerName, triggerGroupName);//      
            sched.deleteJob(jobName, jobGroupName);//     
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:        
     */
    public static void startJobs() {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            sched.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description:        
     *
     */
    public static void shutdownJobs() {
        try {
            Scheduler sched = gSchedulerFactory.getScheduler();
            if (!sched.isShutdown()) {
                sched.shutdown();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String formatTime(Date date){
        //0/1 * * * * ?
        String time = DateUtils.format( date, "s m h d * ?");
        System.out.println(time);
        return time;
    }
}

테스트 클래스
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description:      
 * Created by zhangxb on 2017/2/10.
 */
public class QuartzJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Object params =   context.getJobDetail().getJobDataMap().get("params");
        System.out.println(params);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ "★★★★★★★★★★★");
        System.out.println("【    】  ...");
        QuartzManager.removeJob((String)params);
        System.out.println("【    】  ");
    }
    public static void main(String[] args) {
        try {
            String job_name = "      ";
            System.out.println("【    】  ( 1     )...");
            Calendar cal = Calendar.getInstance();
            cal.add( Calendar.SECOND,10);
            QuartzManager.addJob(job_name, QuartzJob.class, QuartzManager.formatTime(cal.getTime()),job_name);
            Thread.sleep(11);
            cal.setTime( new Date());
            cal.add( Calendar.SECOND,10);
            QuartzManager.addJob(job_name+1, QuartzJob.class, QuartzManager.formatTime(cal.getTime()),job_name+1);
            Thread.sleep(11);
//            System.out.println("【    】  ( 2     )...");
//            QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?");
//            Thread.sleep(6000);
//            System.out.println("【    】  ...");
//            QuartzManager.removeJob(job_name);
//            System.out.println("【    】  ");
//
//            System.out.println("【        】  ( 10     )...");
//            QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?");
//            Thread.sleep(60000);
//            System.out.println("【    】  ...");
//            QuartzManager.removeJob(job_name);
//            System.out.println("【    】  ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

하지만 동적 지연 은 할 수 없 었 다.뒤에 시간 지연 mq 로 이 루어 졌 습 니 다.
다음으로 전송:https://www.cnblogs.com/foreverzxb/p/6491190.html

좋은 웹페이지 즐겨찾기