java 石英不触发简单触发器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14859044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:39:10  来源:igfitidea点击:

Quartz not firing simple trigger

javacronquartz-scheduler

提问by sscarduzio

This should be pretty straight forward, but I see no job being executed. I have a breakpoint on the execute() method of the task, no thread gets there ever. I'm not getting what's wrong.

这应该很简单,但我没有看到正在执行的工作。我在任务的 execute() 方法上有一个断点,没有线程到达那里。我不明白出了什么问题。

the Job

工作

class Printer implements Job{
    public Printer(){
        System.out.println("created printer");
    }

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        System.out.println("hi" + context.getFireTime());
    }

}

The main class

主要类

class MyClass {
      public static void main(String[] args) throws Throwable {
            Scheduler s = StdSchedulerFactory.getDefaultScheduler();
            JobDetail job = newJob(Printer.class).build();
            CronTrigger trigger = 
                    newTrigger()
                    .withIdentity("a", "t")
                    .withSchedule(cronSchedule("0/5 * * * * ?").inTimeZone(TimeZone.getDefault()))
                    .forJob(job).build();
            s.scheduleJob(job, trigger);

// This prints the right date!

            System.out.println(trigger.getNextFireTime()); 
            s.start();
        }
}

EDIT: I discovered I didn't have the quartz.property file, so there was the possibility the threadpool for quartz was not ever created. Therefore as read in documentation, I replaced the code using StdSchedulerFactory with the following:

编辑:我发现我没有quartz.property 文件,所以有可能没有创建quartz 的线程池。因此,如文档中所读,我使用 StdSchedulerFactory 替换了以下代码:

DirectSchedulerFactory.getInstance().createVolatileScheduler(10);
Scheduler s = DirectSchedulerFactory.getInstance().getScheduler();

Guess what? No luck still. Same identical effect. Application keeps staying alive, firing not trigger.

你猜怎么着?仍然没有运气。一样的效果。应用程序保持活动状态,触发而不是触发。

回答by sscarduzio

I found the solution: changing the visibility of the class defining the job (Printer) to publicwill make it possible to Quartz to access it and run it.

我找到了解决方案:将定义作业(打印机)的类的可见性更改为public将使 Quartz 可以访问并运行它。

public class Printer implements Job{ // just add 'public'!
    public Printer(){
        System.out.println("created printer");
    }

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        System.out.println("hi" + context.getFireTime());
    }

}

That's understandable, since it's only possible to pass a <? extends Job>.classto the scheduler (bloody hell, why??) and not - for example - anonymous objects.

这是可以理解的,因为只能将 a 传递<? extends Job>.class给调度程序(该死的,为什么??)而不是 - 例如 - 匿名对象。

Having that said, I find really upsetting the way Quartz silently fails firing jobs without a single error message.

话虽如此,我发现 Quartz 在没有任何错误消息的情况下默默地失败触发作业的方式真的很令人不安。