避免Quartz每次都创建新实例

时间:2018-08-17 17:34:41

标签: java quartz-scheduler

Quartz每次都通过JobBuilder创建类的新实例

JobBuilder.newJob(MyJob.class)

但是,我只需要一个MyJob实例,并且只从execute函数触发testFunction,我该如何使其工作?

我通过QuartzGuiceLib发现可以使用一些注释来实现它,通过Spring我可以在配置文件中进行更改。但是,如何在没有任何框架的情况下通过纯Java实现呢?

下面是代码快照:

public class MyJob implements Job {
    public MyJob() {
        testFunction();

        try {
            final Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            if (!scheduler.checkExists(JOB_KEY)) {
                triggerScheduler(scheduler);
            } else {
                log.info("Job with key {{}} already exist", JOB_KEY);
            }
        } catch (SchedulerException e) {
            log.error("Fail to start scheduler", e);
        }
    }

    public void testFunction() {
    }

    private void triggerScheduler(final Scheduler scheduler) throws SchedulerException {
        final JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity(JOB_KEY)
                .build();

        final Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myjob")
                .withSchedule(
                        simpleSchedule()
                                .withIntervalInSeconds(60)
                                .repeatForever())
                .build();

        scheduler.start();

        log.info("Scheduling job with key {{}}", jobDetail.getKey());

        scheduler.scheduleJob(jobDetail, trigger);
    }

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

1 个答案:

答案 0 :(得分:2)

将作业和调度程序放在两个单独的类中可能会更容易,如下所示:

public class MyQuartzScheduler {
    public static void main( String[] args ) throws Exception {
        JobDetail job = JobBuilder.newJob(MyJob.class)
        .withIdentity("dummyJobName", "group1").build();

        Trigger trigger = TriggerBuilder
        .newTrigger()
        .withIdentity("MyJobTrigger", "group1")
        .withSchedule(
            CronScheduleBuilder.cronSchedule("0 * * * * ?"))
        .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
    }
}

然后是您的工作类别:

public class MyJob implements Job {    
    public void testFunction() {
        System.out.println("Running Test!");
    }

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

这是来自mkyong教程文章的改编,该文章位于:

https://www.mkyong.com/java/quartz-2-scheduler-tutorial/

对于您的问题的答案,Quartz每次运行都会创建一个新实例: https://stackoverflow.com/a/10463309/1410671

您可以创建另一个静态类或Job类将调用的Factory,它在每次调用时都使用相同的实例。

public class MyJob implements Job {

    public void testFunction() {
        MyClassWithStaticCounter.increaseCounter(1);
        System.out.println(MyClassWithStaticCounter.getCounter());
    }

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

还有具有静态内容的类:

public class MyClassWithStaticCounter {
    private static int counter = 0;

    public static void increaseCounter(int i){
        counter += i;
    }

    public static int getCounter(){
        return counter;
    }
}