Quartz Scheduler for java- How to run job every 5 minutes

43.5k views Asked by At

Friends, I am using quartz scheduler for running a task every 5 minutes starting when application deployed & running continuously so i have written code as:

SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sche = sf.getScheduler();

JobDetail job = newJob(RomeJob.class).withIdentity("Id1", "Rome").build();
CronTrigger trigger = newTrigger().withIdentity("Id1Trigger", "Rome").withSchedule(cronSchedule("0 0/5 * * * ?"))
.build();
sche.scheduleJob(job, trigger);
sche.start();

But its working sometime sometimes not. Please tell me whether i am missing something here?

4

There are 4 answers

0
Umesh Awasthi On BEST ANSWER

You have many ways one of them is use trigger builder something like

trigger = newTrigger()
    .withIdentity("mytrigger", "group1")
    .startNow()
    .withSchedule(simpleSchedule()
            .withIntervalInMinutes(5)
            .repeatForever())
    .build();
5
AudioBubble On

Instead of

0 0/5 * * * ?

use

0 */5 * * * *

Edit: This results in your task being run at 0 seconds of every minute that is divisible by 5.

Edit 2: 0/5 means only the minutes 0 and 5.

1
Tomasz Nurkiewicz On

Do not use Cron schedule but simple schedule instead:

Trigger trigger = newTrigger().
  withIdentity("Id1Trigger", "Rome").
  withSchedule(
    simpleSchedule().
      withIntervalInMinutes(5).
      repeatForever()
  ).build();
0
Samreach On

Now updated to new version!

            /* Instantiate the job that will call the bot function */
            JobDetail jobSendNotification = JobBuilder.newJob(SendNotification.class)
                    .withIdentity("sendNotification")
                    .build();

            /* Define a trigger for the call */
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("sendEvery5Minutes")
                    .withSchedule(
                            SimpleScheduleBuilder.repeatMinutelyForever(5))
                    .build();

            /* Create a scheduler to manage triggers */
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.getContext().put("bot", bot);
            scheduler.start();
            scheduler.scheduleJob(jobSendNotification, trigger);

Hope this will help someone find new the version. Send repeatMinute, repeatHourly