Hatem Salem
Hatem Salem

Reputation: 195

Quartz runs the job once

Am using Quartz framework to schedule jobs. I want to repeat the operation every 2 seconds. But it seems that it executes once only. How I can make it run forever !

public static void main(String[] args) throws InterruptedException {

    //ChangingRatesSimulator.doSimulation();
    try {
        // Grab the Scheduler instance from the Factory 
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // and start it off
        scheduler.start();
        JobDetail job = newJob(ChangingRatesSimulator.class).withIdentity("job1","group1").build();

        // compute a time that is on the next round minute
        Date runTime =  evenSecondDate(new Date());

        // Trigger the job to run on the next round minute
        Trigger trigger = newTrigger()
            .withIdentity("trigger1", "group1")
            .startAt(runTime)
            .withSchedule(simpleSchedule()
                    .withIntervalInSeconds(5)
                    .repeatForever())
            .build();
        scheduler.scheduleJob(job, trigger);
        scheduler.start();





    } catch (SchedulerException se) {
        se.printStackTrace();
    }

}  

Upvotes: 0

Views: 1636

Answers (1)

abalogh
abalogh

Reputation: 8281

Since you tagged your question with Spring, I assume you are using it. You should check out the changes done in Spring 3 regarding scheduling here.

On the other hand of course it can be achieved with pure Quartz, probably I'd go with a cron expression for the CronTrigger, something like this:

0/2 * * * * *

Upvotes: 1

Related Questions