quartz定时任务不执行不报错处理

用开源定时任务框架Quartz搭建定时任务,任务启动后无任何错误信息输出,也不执行任务。

quartz版本

        
            org.quartz-scheduler
            quartz
            2.3.0
        

quartz schedule 调度器核心代码

/**
 * 定时任务调度器
 */
public class QuartzSchedule {

    private static Logger logger = LoggerFactory.getLogger(QuartzSchedule.class);

    private static Scheduler scheduler;

    static {
        try {
            scheduler = StdSchedulerFactory.getDefaultScheduler();
            initTimer();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public QuartzSchedule() throws SchedulerException {
    }

    /**
     * 初始化定时任务
     *
     * @throws SchedulerException
     */
    private static void initTimer() {
        // and start it off
        try {
            JobDetail jobDetail = new JobDetailImpl();
            //定时任务名称,指定属于哪一个定时任务
            JobKey jobKey = new JobKey("Job1");
            ((JobDetailImpl) jobDetail).setKey(jobKey);
            ((JobDetailImpl) jobDetail).setJobClass(QuartzTask.class);

            JobDataMap jobDataMap = new JobDataMap();
            IQuartzTaskProcess process = new QuartzTaskProcess();
            jobDataMap.put("attachment", process);
            jobDataMap.put("className", "com.job.TestJob1");
            jobDataMap.put("methodName", "doTask");
            ((JobDetailImpl) jobDetail).setJobDataMap(jobDataMap);
            //任务触发器
            Trigger trigger = new CronTriggerImpl();
            ((CronTriggerImpl) trigger).setCronExpression("*/5 * * * * ?");
            ((CronTriggerImpl) trigger).setName("trigger");
            ((CronTriggerImpl) trigger).setJobKey(jobKey);

            scheduler.scheduleJob(jobDetail, trigger);
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    //定时任务主调
    public static void start() throws Exception {
        if (ZkUtils.lock("/timer")) {
            boolean started = scheduler.isStarted();
            if (!started) {
                System.out.println("start job ");
                try {
                    scheduler.start();
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void pauseJob(JobKey jobKey) throws SchedulerException {
        System.out.println("pauseJob jobName : " + jobKey.getName());
        scheduler.pauseJob(jobKey);
    }

    public static void resumeJob(JobKey jobKey) throws SchedulerException {
        System.out.println("resumeJob jobName : " + jobKey.getName());
        scheduler.resumeJob(jobKey);
    }

    public static void removeJob(JobKey jobKey) throws SchedulerException {
        scheduler.deleteJob(jobKey);
    }
}
QuartzTask类如下
public class QuartzTask implements Job {

    private IQuartzTaskProcess quartzTaskProcess;

    public QuartzTask(IQuartzTaskProcess process) {
        this.quartzTaskProcess = process;
    }
    public void execute(JobExecutionContext context)  {
        try {
            //执行业务逻辑
            //可以通过context将调用方法的类信息传递过来,通过反射调用任务
            IQuartzTaskProcess quartzTaskProcess = (IQuartzTaskProcess) context.getJobDetail().getJobDataMap().get("attachment");
            IQuartzJobParam param = new QuartzJobParam();
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            ((QuartzJobParam) param).setClassName(jobDataMap.getString("className"));
            ((QuartzJobParam) param).setMethodName(jobDataMap.getString("methodName"));
            quartzTaskProcess.processJob(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

启动测试

    @Test
    public void testSchedule() throws Exception {
        QuartzSchedule.start();
        System.in.read();
    }

运行情况如下

quartz定时任务不执行不报错处理_第1张图片

发现定时任务启动了,但是未执行task任务,断点跟踪源码发现报错了,但是没有错误日志输出,有可能由于没有日志的配置文件导致quartz框架日志没有输出,于是在config下log4j.properties文件,内容如下

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n 

继续执行,如下

quartz定时任务不执行不报错处理_第2张图片

有错误日志了,是SimpleJobFatory的newJob出现异常,跟踪源码

    public Job newJob(TriggerFiredBundle bundle, Scheduler Scheduler) throws SchedulerException {
        JobDetail jobDetail = bundle.getJobDetail();
        Class jobClass = jobDetail.getJobClass();

        try {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Producing instance of Job '" + jobDetail.getKey() + "', class=" + jobClass.getName());
            }

            return (Job)jobClass.newInstance();
        } catch (Exception var7) {
            SchedulerException se = new SchedulerException("Problem instantiating class '" + jobDetail.getJobClass().getName() + "'", var7);
            throw se;
        }
    }

发现是jobClass.newInstance()出现异常,返回去检查我们写的QuartzTask类,果然有问题,修改继续测试

quartz定时任务不执行不报错处理_第3张图片

任务正常执行。。。

你可能感兴趣的:(quartz定时任务不执行不报错处理)