定时器,简单记录

定时器学习笔记,记录一下。
这里使用的jar包是:quartz-all-1.6.0 jar
使用定时器的jar时,需要引用其他的依赖包
com.springsource.org.apache.commons.beanutils-1.8.0.jar
com.springsource.org.apache.commons.collections-3.2.1.jar
com.springsource.org.apache.commons.digester-1.8.1.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
jta-1.1.jar
quartz-all-1.6.0.jar
才能正常的启动。

package com.lsh.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class SchedulerJod {
    /**
     * 添加一个定时任务
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     * @patam className
     * @param time
     * @throws SchedulerException 
     */
    public boolean addJob(String jobName,String jobGroupName,String triggerName,
            String triggerGroupName,String className,String time) throws SchedulerException{
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();//真实的开发工程中,用注解注入scheduler,或者用xml配置文件获取bean
        boolean flag = false;
        try {
            Class<?> clazz = Class.forName(className);
            JobDetail jobDetail = new JobDetail(jobName, jobGroupName, clazz);
            CronTrigger cronTrigger = new CronTrigger(triggerName, triggerGroupName);
            cronTrigger.setCronExpression(time);
            scheduler.scheduleJob(jobDetail, cronTrigger);
            if(!scheduler.isShutdown()){
                System.out.println("scheduler 的状态为--->"+scheduler.isShutdown());
                scheduler.start();
                flag = true;
            }

        } catch (ClassNotFoundException e) {
            System.out.println("启动定时任务失败");
            e.printStackTrace();
        } catch (ParseException e) {
            System.out.println("启动定时任务失败");
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 批量插入任务job
     * @param list 任务列表list集合
     */
    public void startJob(List<TaskJob> list){
        if(list == null || list.size() == 0){
            System.out.println("无定时队列");
            return; 
        }
        for(TaskJob taskJob : list){
            String jobName = taskJob.getJobName();
            String jobGroupName = taskJob.getJobGroupname();
            String className = taskJob.getClassName();
            String cronExpression = taskJob.getCronExpression();
            try {
                boolean flag = addJob(jobName, jobGroupName, jobName+"Trigger",jobGroupName+"Trigger" , className, cronExpression);
                if(flag){
                    System.out.println(jobName+"插入任务池成功!");

                }else{
                    System.out.println("插入任务池失败");
                }
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 移除定时任务,使用默认组名
     * @param args
     * @throws SchedulerException
     * @throws ParseException
     * @throws InterruptedException
     */
    public void removeJob(String jobName, String jobGroup) throws SchedulerException{
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.pauseTrigger(jobName+"Trigger", jobGroup);
        scheduler.deleteJob(jobName+"Trigger", jobGroup);
        System.out.println("移除任务成功");

    }
    /**
     * 暂停所有的任务
     * @param args
     * @throws SchedulerException
     * @throws ParseException
     * @throws InterruptedException
     */
    public void pauseAllJob(){
        try {
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.pauseAll();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }
    /**关闭所有的定时任务
     * @throws SchedulerException */
    public void shutdownJobs() throws SchedulerException{
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        if(!scheduler.isShutdown()){
            scheduler.shutdown();
        }
        System.err.println("关闭定时任务成功!");
    }
    /**
     * 唤醒所有的任务
     * @param args
     * @throws SchedulerException
     * @throws ParseException
     * @throws InterruptedException
     */
    public void modifyJobTime(String jobName,String time) throws SchedulerException{
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(jobName, null);
        if(trigger == null){
            return;
        }
        String oldTime = trigger.getCronExpression();
        if(!oldTime.equalsIgnoreCase(time)){
            JobDetail jobDetail = scheduler.getJobDetail(jobName, "");
            Class ObjJobClass = jobDetail.getJobClass();
            removeJob(jobName, "jobGroup");
            addJob(jobName, "jobGroupName", "triggerName", "triggerGroupName", "className", "oldTime");
        }
    }
    public static void main(String[] args) throws SchedulerException, 
    ParseException, InterruptedException {
        //Scheduler scheduler = new StdSchedulerFactory()
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        String date = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss").format(new Date());
        String job_name ="定时任务"+date;
        JobDetail jobDetail = new JobDetail(job_name, "testJobGroup", quartzJobTest.class);
        CronTrigger cronTrigger = new CronTrigger(job_name+"Trigger", "testJobGroup");
        cronTrigger.setCronExpression("1/1 * * * * ?");
        scheduler.scheduleJob(jobDetail, cronTrigger);
        if(!scheduler.isShutdown()){
            System.out.println("scheduler状态---》"+!scheduler.isShutdown());
            scheduler.start();
        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
        Thread.sleep(6000);
        /*scheduler.pauseTrigger(job_name+"Trigger", "testJobGroup");//暂停触发器
        scheduler.unscheduleJob(job_name+"Trigger", "testJobGroup");//移除触发器
        scheduler.deleteJob(job_name, "testJobGroup");
        System.out.println(job_name + "--->删除任务成功!!");
        */
        jobDetail = new JobDetail(job_name+"Test1", "testJobGroup", quartzJobTest.class);
        cronTrigger = new CronTrigger(job_name+"test1"+"Tigger", "testJobGroup");
        cronTrigger.setCronExpression("1/1 * * * * ?");
        scheduler.scheduleJob(jobDetail, cronTrigger);
        if(!scheduler.isShutdown()){
            System.out.println("scheduler状态---》"+!scheduler.isShutdown());
            scheduler.start();
        }

        //暂停任务job_name+test任务
        Thread.sleep(5 * 1000);
        scheduler.pauseJob(job_name+"Test1", "testJobGroup");
        System.out.println("---------------------------------------------暂停任务成功!!!");

        Thread.sleep(5 * 1000);
//      唤醒job_name+test任务
        scheduler.resumeJob(job_name+"Test1","testJobGroup" );
        System.out.println("------------------------------------------------唤醒任务成功!!!!");

        //
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页