1

In my quartz scheduler my Main class is executed 5 or 6 times when a cron trigger firedin every 20 seconds.I want to restrict to execute Main class only once when cron trigger fired in every 20 seconds

This is my CronTrigger

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Main.class).build();
Date startTime = DateBuilder.nextGivenSecondDate(null, 10);
// run every 20 seconds infinite loop
CronTrigger crontrigger = TriggerBuilder.newTrigger().startAt(startTime).withSchedule(CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).build();
scheduler.start();
scheduler.scheduleJob(job, crontrigger);

This is Main Class method

Main.java

public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println("Trigger Starts.."); 
}

My actual output is

Trigger 1 - When cron fires first time it will execute 3 or 4 times

Trigger Starts..

Trigger Starts..

Trigger Starts..

Trigger 2 - When cron fires second time it will execute 6 or 7 times

Trigger Starts..

Trigger Starts..

Trigger Starts..

Trigger Starts..

Trigger Starts..

Trigger Starts..

Trigger Starts..

Trigger Starts..

6 or 7 times this Main class is called. I want to restrict this situation

My expected output is

Trigger 1 (first 20 sec)

Trigger Starts..

Trigger 2 (next 20 sec)

Trigger Starts..

It should be executed once

Arav KT
  • 301
  • 6
  • 18

1 Answers1

0

On my system, I tested it and it works perfectly fine. Can you please use the following template and please share it's output and some info about logger if used.

Job:

public void execute(JobExecutionContext context) throws JobExecutionException
{
    int count;
    if(context.get("count") == null)
        context.put("count", count = 1);
    else
        context.put("count", count = (Integer) context.get("count") + 1);
    System.out.println("Hello Quartz!, Count: " + count);
}

This is my CronTrigger:

SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler scheduler = sf.getScheduler();
    JobDetail job = JobBuilder.newJob(HelloJob.class).build();
    // run every 20 seconds infinite loop
    CronTrigger crontrigger = TriggerBuilder.newTrigger().withSchedule(
            CronScheduleBuilder.cronSchedule("0/20 * * * * ?")).build();
    scheduler.start();
    scheduler.scheduleJob(job, crontrigger);
Harneet Singh
  • 2,328
  • 3
  • 16
  • 23
  • first time ok but next 20 seconds it may reccur more than 6 times – Arav KT Jul 18 '17 at 10:40
  • check it , am i corrrect Ji – Arav KT Jul 18 '17 at 10:42
  • I am getting this for instance: System.out.println("Hello Quartz!, " + new Date()); Hello Quartz!, Tue Jul 18 16:14:20 IST 2017 Hello Quartz!, Tue Jul 18 16:14:40 IST 2017 Hello Quartz!, Tue Jul 18 16:15:00 IST 2017 Hello Quartz!, Tue Jul 18 16:15:20 IST 2017 – Harneet Singh Jul 18 '17 at 10:46
  • I am using this cron Expression 0 0/1 * 1/1 * ? * every one minute – Arav KT Jul 18 '17 at 10:50
  • but it will execute more than once – Arav KT Jul 18 '17 at 10:53
  • public void execute(JobExecutionContext context) throws JobExecutionException { int count; if (context.get("count") == null) context.put("count", count = 1); else context.put("count", count = (Integer) context.get("count") + 1); try { System.out.println("JOB STARTED.....!"); queSub.startGrapReportProcessor(USER); } catch (BaseException | IOException e) { e.printStackTrace(); } } – Arav KT Jul 18 '17 at 11:04
  • this is my job method – Arav KT Jul 18 '17 at 11:05
  • No No, am asking how are you executing the program? means via eclipse? or by terminal? or you have it in tomcat/anyServer? point here for asking is i need to know if there is any previous context exists which interferes. and try kill all pids of java via jps which you think you are not using.... – Harneet Singh Jul 18 '17 at 11:13
  • I am excuting via eclipse – Arav KT Jul 18 '17 at 11:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149473/discussion-between-arav-kt-and-harneet-singh). – Arav KT Jul 18 '17 at 11:20