简体   繁体   中英

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

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?

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.

Do not use Cron schedule but simple schedule instead:

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

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM