NutzCN Logo
问答 nutzboot 实现动态修改定时任务
发布于 2122天前 作者 为什么我还是渣渣 2011 次浏览 复制 上一个帖子 下一个帖子
标签:

[ERROR] 11:25:00.649 org.quartz.core.JobRunShell.run(JobRunShell.java:211) - Job XLXXCC_JOB_GROUP.动态任务调度 threw an unhandled Exception:
java.lang.NullPointerException
at com.yc.job.SampleJob.execute(SampleJob.java:25)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[ERROR] 11:25:00.664 org.quartz.core.ErrorLogger.schedulerError(QuartzScheduler.java:2401) - Job (XLXXCC_JOB_GROUP.动态任务调度 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
at com.yc.job.SampleJob.execute(SampleJob.java:25)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
... 1 more

@IocBean(create="init", depose="depose")
public class MainLauncher {

@Inject
protected PropertiesProxy conf;

@At("/")
@Ok("->:/index.html")
public void index() {}

public void init() {
    // NB自身初始化完成后会调用这个方法
    QuartzManager.addJob(QuertzConstans.JOB_NAME, QuertzConstans.JOB_GROUP_NAME, QuertzConstans.TRIGGER_NAME, QuertzConstans.TRIGGER_GROUP_NAME, SampleJob.class, "0/1 * * * * ?");
}
public void depose() {

}

public static void main(String[] args) throws Exception {
    new NbApp().setArgs(args).setPrintProcDoc(true).run();
}

}

@IocBean
public class SampleJob implements Job {

@Inject
RepayReminderService repayReminderService;

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {

    QuartzManager.modifyJobTime(QuertzConstans.JOB_NAME, QuertzConstans.JOB_GROUP_NAME, QuertzConstans.TRIGGER_NAME, QuertzConstans.TRIGGER_GROUP_NAME, "0/5 * * * * ?");
    repayReminderService.OneDayRepayReminder();
    System.out.println("time now=" + System.currentTimeMillis());
}

}

9 回复

25行是哪一行?

repayReminderService.OneDayRepayReminder();

那就是说repayReminderService为null了? 看来是注入问题

是的 只有动态配置定时任务在初始化的时候才会出现这样的问题

贴一下QuartzManager.addJob方法, 记得用"插入代码"按钮

package com.yc.util;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzManager {

    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();  

    /** 
     * @Description: 添加一个定时任务 
     *  
     * @param jobName 任务名 
     * @param jobGroupName  任务组名 
     * @param triggerName 触发器名 
     * @param triggerGroupName 触发器组名 
     * @param jobClass  任务 
     * @param cron   时间设置,参考quartz说明文档  
     */  
    @SuppressWarnings({ "unchecked", "rawtypes" })  
    public static void addJob(String jobName, String jobGroupName, 
            String triggerName, String triggerGroupName, Class jobClass, String cron) {  
        try {  
            Scheduler sched = schedulerFactory.getScheduler();  
            // 任务名,任务组,任务执行类
            JobDetail jobDetail= JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();

            // 触发器  
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            // 触发器名,触发器组  
            triggerBuilder.withIdentity(triggerName, triggerGroupName);
            triggerBuilder.startNow();
            // 触发器时间设定  
            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
            // 创建Trigger对象
            CronTrigger trigger = (CronTrigger) triggerBuilder.build();

            // 调度容器设置JobDetail和Trigger
            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 cron   时间设置,参考quartz说明文档   
     */  
    public static void modifyJobTime(String jobName, 
            String jobGroupName, String triggerName, String triggerGroupName, String cron) {  
        try {  
            Scheduler sched = schedulerFactory.getScheduler();  
            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
            CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey);  
            if (trigger == null) {  
                return;  
            }  

            String oldTime = trigger.getCronExpression();  
            if (!oldTime.equalsIgnoreCase(cron)) { 
                /** 方式一 :调用 rescheduleJob 开始 */
                // 触发器  
                TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
                // 触发器名,触发器组  
                triggerBuilder.withIdentity(triggerName, triggerGroupName);
                triggerBuilder.startNow();
                // 触发器时间设定  
                triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
                // 创建Trigger对象
                trigger = (CronTrigger) triggerBuilder.build();
                // 方式一 :修改一个任务的触发时间
                sched.rescheduleJob(triggerKey, trigger);
                /** 方式一 :调用 rescheduleJob 结束 */

                /** 方式二:先删除,然后在创建一个新的Job  */
                //JobDetail jobDetail = sched.getJobDetail(JobKey.jobKey(jobName, jobGroupName));  
                //Class<? extends Job> jobClass = jobDetail.getJobClass();  
                //removeJob(jobName, jobGroupName, triggerName, triggerGroupName);  
                //addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron); 
                /** 方式二 :先删除,然后在创建一个新的Job */
            }  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  

    /** 
     * @Description: 移除一个任务 
     *  
     * @param jobName 
     * @param jobGroupName 
     * @param triggerName 
     * @param triggerGroupName 
     */  
    public static void removeJob(String jobName, String jobGroupName,  
            String triggerName, String triggerGroupName) {  
        try {  
            Scheduler sched = schedulerFactory.getScheduler();  

            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);

            sched.pauseTrigger(triggerKey);// 停止触发器  
            sched.unscheduleJob(triggerKey);// 移除触发器  
            sched.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  

    /** 
     * @Description:启动所有定时任务 
     */  
    public static void startJobs() {  
        try {  
            Scheduler sched = schedulerFactory.getScheduler();  
            sched.start();  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  

    /** 
     * @Description:关闭所有定时任务 
     */  
    public static void shutdownJobs() {  
        try {  
            Scheduler sched = schedulerFactory.getScheduler();  
            if (!sched.isShutdown()) {  
                sched.shutdown();  
            }  
        } catch (Exception e) {  
            throw new RuntimeException(e);  
        }  
    }  
}
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();  

这样写是新建一个Quartz调度实体了, 跟nutz集成好的不是同一个的.

要走Ioc

// QuartzManager  类加上@IocBean, 并换成这个属性scheduler, 去掉SchedulerFactory , 
@Inject
public Scheduler scheduler;

使用QuartzManager 的地方用注入的方式获取

直接new StdSchedulerFactory得到的SchedulerFactory 和 Scheduler 是没有设置JobFactory的, 没有Job类的注入功能

添加回复
请先登陆
回到顶部