java-一个定时任务工具(Quartz)

package com.base.util;

import org.apache.commons.lang.time.DateUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 定时任务管理类
 */
public class QuartzTaskUtil {

    private SchedulerFactory schedulerFactory = new StdSchedulerFactory();

    private static QuartzTaskUtil quartzTaskUtil;

    private static ReentrantLock lock = new ReentrantLock();

    /*--------------------------------------------
     |         将饿汉模式改成双重锁模式           |
     ============================================*/
    public static QuartzTaskUtil getInstance() {
        if (quartzTaskUtil == null) {
            // 上锁
            lock.lock();
            try {
                if (quartzTaskUtil == null) {
                    quartzTaskUtil = new QuartzTaskUtil();
                }
            } finally {
                // 下锁
                lock.unlock();
            }
        }

        return quartzTaskUtil;
    }

    public QuartzTaskUtil() {
    }

    /**
     * @param jobName          任务名
     * @param jobGroupName     任务组名
     * @param triggerName      触发器名
     * @param triggerGroupName 触发器组名
     * @param jobClass         任务
     * @param time             时间设置,参考quartz说明文档
     *                         添加一个定时任务
     */
    public JobDetail addJob(String jobName, String jobGroupName,
                            String triggerName, String triggerGroupName, Class<? extends Job> jobClass,
                            String time, JobDataMap params) {
        JobDetail detail = null;
        try {
            Scheduler sch = schedulerFactory.getScheduler();

            CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);
            trigger.setCronExpression(time);

            detail = new JobDetail(jobName, jobGroupName, jobClass);
            detail.setJobDataMap(params);

            sch.scheduleJob(detail, trigger);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return detail;
    }

    public JobDetail addJob(String jobName, String jobGroupName,
                            String triggerName, String triggerGroupName, Class<? extends Job> jobClass,
                            int seconds, JobDataMap params) {
        JobDetail detail = null;
        try {
            Scheduler sch = schedulerFactory.getScheduler();

            SimpleTrigger trigger = new SimpleTrigger(
                    triggerName,
                    triggerGroupName,
                    jobName,
                    jobGroupName,
                    DateUtils.addSeconds(new Date(), seconds),
                    null,
                    -1,
                    seconds * 1000L);

            detail = new JobDetail(jobName, jobGroupName, jobClass);
            detail.setJobDataMap(params);

            sch.scheduleJob(detail, trigger);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return detail;
    }

    /**
     * @param triggerName
     * @param triggerGroupName
     * @param time             修改一个任务的触发时间
     */
    public void modifyJobTime(String triggerName, String triggerGroupName, String time) {
        try {
            Scheduler sch = schedulerFactory.getScheduler();
            CronTrigger trigger = (CronTrigger) sch.getTrigger(triggerName, triggerGroupName);
            if (trigger == null) {
                return;
            }

            String oldTime = trigger.getCronExpression();
            if (!oldTime.equalsIgnoreCase(time)) {
                trigger.setCronExpression(time);
                sch.resumeTrigger(triggerName, triggerGroupName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName 移除一个任务
     */
    public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
        try {
            Scheduler sch = schedulerFactory.getScheduler();
            sch.pauseTrigger(triggerName, triggerGroupName);
            sch.unscheduleJob(triggerName, triggerGroupName);
            sch.deleteJob(jobName, jobGroupName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动所有定时任务
     */
    public void startJobs() {
        try {
            Scheduler sch = schedulerFactory.getScheduler();
            if (!sch.isStarted()) {
                sch.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭所有定时任务
     */
    public void shutdownJobs() {
        try {
            Scheduler sch = schedulerFactory.getScheduler();
            if (!sch.isShutdown()) {
                sch.shutdown();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void info() {
        try {
            Scheduler sch = schedulerFactory.getScheduler();
            String[] groups = sch.getJobGroupNames();
            for (String group : groups) {
                String[] jobs = sch.getJobNames(group);
                for (String job : jobs) {
                    System.out.println(group + ":" + job);
                }
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}  

阅读更多
版权声明: https://blog.csdn.net/qq_35045184/article/details/80330656
文章标签: it quartz
个人分类: java-web学习笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭