jquartz的job触发demo

package com.ancun.tdbank.manage.utils;

import com.ancun.tdbank.model.TdTask;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JobUtil {

    private static final Logger logger = LoggerFactory.getLogger(JobUtil.class);
    private static Scheduler scheduler;
    private static final String  perfix = "com.ancun.tdbank.manage.system.task.";

    private static Scheduler getScheduler(){
        if (scheduler == null){
            try {
                scheduler = StdSchedulerFactory.getDefaultScheduler();
                logger.info("创建scheduler实例成功");
            } catch (SchedulerException e) {
                logger.info("创建scheduler实例失败");
                e.printStackTrace();
            }
        }
        return scheduler;
    }

    /**
     * 触发任务
     * @param tdTask
     * @throws Exception
     */
    public static void addJob(TdTask tdTask)throws Exception{
        Scheduler scheduler = getScheduler();
        logger.info("开始触发任务");
        String jobClassName = perfix + tdTask.getJobClass();
        String jobGroupName = tdTask.getGroupName();
        String cronExpression = tdTask.getCornExpr();
        logger.info("启动调度器");
        scheduler.start();
        logger.info("构建job信息");
        JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName)).withIdentity(jobClassName, jobGroupName).build();

        logger.info("表达式调度构建器(即任务执行的时间)");
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
        logger.info("按新的cronExpression表达式构建一个新的trigger");
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
                .withSchedule(scheduleBuilder).build();
        try {
            scheduler.scheduleJob(jobDetail, trigger);
            logger.info("创建定时任务成功");
        } catch (SchedulerException e) {
            logger.info("创建定时任务失败,失败信息{}",e);
            logger.error("创建定时任务失败,失败信息{}",e);
        }
    }

    /**
     * 暂停任务
     * @param tdTask
     * @throws Exception
     */
    public static void pausejob(TdTask tdTask) throws Exception {
        Scheduler scheduler = getScheduler();
        scheduler.pauseJob(JobKey.jobKey(tdTask.getJobClass(),tdTask.getGroupName()));
    }

    /**
     * 恢复任务
     * @param tdTask
     * @throws Exception
     */
    public static void resumejob(TdTask tdTask) throws Exception {
        Scheduler scheduler = getScheduler();
        scheduler.resumeJob(JobKey.jobKey(tdTask.getJobClass(),tdTask.getGroupName()));
    }

    /**
     * 重新排定日程
     * @param tdTask
     * @throws Exception
     */
    public static void rescheduleJob(TdTask tdTask) throws Exception {
        Scheduler scheduler = getScheduler();
        logger.info("开始重新排定日程");
        String jobClassName = tdTask.getJobClass();
        String jobGroupName = tdTask.getGroupName();
        String cronExpression = tdTask.getCornExpr();
        TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
        logger.info("表达式调度构建器");
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        logger.info("按新的cronExpression表达式重新构建trigger");
        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

        logger.info("按新的trigger重新设置job执行");
        scheduler.rescheduleJob(triggerKey, trigger);
    }

    /**
     * 删除任务
     * @param tdTask
     * @throws Exception
     */
    public static void deletejob(TdTask tdTask) throws Exception {
        Scheduler scheduler = getScheduler();
        logger.info("开始重新排定日程");
        String jobClassName = tdTask.getJobClass();
        String jobGroupName = tdTask.getGroupName();
        scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
        scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
        scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
    }


    private static Class getClass(String classname){
        logger.info("获取以String类型的className实例化类");
        Class obj= null;
        try {
            obj = Class.forName(classname);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return obj;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值