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.
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:
DirectSchedulerFactory.getInstance().createVolatileScheduler(10);
Scheduler s = DirectSchedulerFactory.getInstance().getScheduler();
Guess what? No luck still. Same identical effect. Application keeps staying alive, firing not trigger.
BLOCKED = the trigger is prevented from being fired because it relates to a StatefulJob that is already executing. When the statefuljob completes its execution, all triggers relating to that job will return to the WAITING state.
Trigger - a component that defines the schedule upon which a given Job will be executed. JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.
I found the solution: changing the visibility of the class defining the job (Printer) to public will make it possible to Quartz to access it and run it.
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>.class
to the scheduler (bloody hell, why??) and not - for example - anonymous objects.
Having that said, I find really upsetting the way Quartz silently fails firing jobs without a single error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With