Dhaval Bhoot
Dhaval Bhoot

Reputation: 251

Quartz scheduler job get not executed repeatedly for every 5 minute

I have code to schedule a job to execute at every 5 minutes. The code is as follow

JobDetail jobDetail = JobBuilder.newJob(ChieldJob.class).withIdentity(("job_"+sched_id+"_"+schd_name), "todaysJob")
                            .setJobData(chieldJobData).build();

The code to make trigger is as follow.

Trigger trigger = TriggerBuilder.newTrigger().withIdentity(("trg_" + sched_id +"_"+schd_name), "todaysJob")
                            .startAt(new Date(row.get(field_Schd_Start_TS).longValue()))
                            .build();
trigger = trigger.getTriggerBuilder().withSchedule((ScheduleBuilder) SimpleScheduleBuilder.repeatMinutelyForever(5)).build();

Now I schedule a job by below code

context.getScheduler().scheduleJob(jobDetail,trigger);

Here the context is the object of JobExecutionContext, because, I am initiating this job from other job execution class. getScheduler() method will return the same scheduler instance from which the main job gets executed

The problem is, this job in "jobDetail" object is get executed only once, at a specified time, specified by ".startAt(new Date(row.get(field_Schd_Start_TS)" then it do not get executed at every 5 minutes. I am not able to find out why it does not get executed repeatedly.

The nextFireTime is null, in the trigger. why? I am not able to find out this. I make a small program in the main method to schedule job, here I see that, when the job gets executed, we get the JobExecutionContext object in that job, here I debug and see it contains the object of the trigger, which has nextFireTime set as expected. But that do not happen in actual implementaion of that code where I use.

Upvotes: 0

Views: 2662

Answers (1)

chocksaway
chocksaway

Reputation: 900

I have a found a nice example at https://www.concretepage.com/scheduler/quartz/quartz-2-scheduler-simpletrigger-example-with-simpleschedulebuilder

I have got "some" sample code working OK (I have used .repeatSecondlyForever, as I wanted quick debug output):

import org.quartz.JobBuilder;
import org.quartz.JobDetail;  
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Calendar;
import java.util.Date;

public class SimpleTriggerExample {
    public static void main(String[] args) throws SchedulerException {
         SchedulerFactory schedulerFactory = new StdSchedulerFactory();
         Scheduler scheduler = schedulerFactory.getScheduler();
         JobDetail jobDetail =     JobBuilder.newJob(PrintDateJob.class).withIdentity("goodjob", "mygroup").build();

         SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("goodjob", "mygroup")
            .startAt(new Date(Calendar.getInstance().getTimeInMillis() + 3000))
            .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
            .build();

        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();

        try {
            //wait to finish the job
            Thread.sleep(200000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //shutdown scheduler gracefully
        scheduler.shutdown(true);
    }
}

The "PrintDateJob.class" is here:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;

import java.util.Date;

public class PrintDateJob implements Job {
    public void execute(JobExecutionContext context)
        throws JobExecutionException {
        JobKey jobKey = context.getJobDetail().getKey();
        System.out.println(jobKey+": "+ new Date());
    }
}

pom.xml is here:

<?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>chocksaway</groupId>
        <artifactId>001</artifactId>
        <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
    </dependencies>
</project>

Running the example gave the following output:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
mygroup.goodjob: Thu Jun 21 15:15:53 BST 2018
mygroup.goodjob: Thu Jun 21 15:15:58 BST 2018
mygroup.goodjob: Thu Jun 21 15:16:03 BST 2018
mygroup.goodjob: Thu Jun 21 15:16:08 BST 2018

Upvotes: 0

Related Questions