4

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JMohasin
  • 513
  • 2
  • 13
  • 35

4 Answers4

13

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.

  • thanks for quick responce , but i want to run my task every 5 minutes continuously will you helo to figure out cron string for this – JMohasin Feb 01 '12 at 12:54
  • What do you mean by "every 5 minutes continuously"? –  Feb 01 '12 at 12:55
  • 1
    once i deploy my website on tomcat task should run continuously with interval of 5 minutes it should not stop until tomcat stop but with interval of 5 minutes – JMohasin Feb 01 '12 at 12:56
  • 4
    In latest version of Quartz: ' 0 */5 * * * ? ' – user2171669 May 13 '20 at 18:37
  • @user2171669 Not everyone is running the latest version. Our shop uses the top format. – mckenzm Dec 12 '22 at 03:45
8

Do not use Cron schedule but simple schedule instead:

Trigger trigger = newTrigger().
  withIdentity("Id1Trigger", "Rome").
  withSchedule(
    simpleSchedule().
      withIntervalInMinutes(5).
      repeatForever()
  ).build();
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • +1 since the OP asked for "every 5 minutes starting when application deployed", not every 5 minutes on the wallclock. –  Feb 01 '12 at 12:54
0

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

Samreach
  • 81
  • 1
  • 7
0

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();
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204