Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz not firing simple trigger

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.

like image 905
sscarduzio Avatar asked Feb 13 '13 17:02

sscarduzio


People also ask

Why is my quartz trigger blocked?

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.

What is trigger in quartz?

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.


1 Answers

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.

like image 77
sscarduzio Avatar answered Nov 15 '22 20:11

sscarduzio