Ali-Alrabi
Ali-Alrabi

Reputation: 1698

quartz fixed number of times

I have the following spring quartz configuration. It works fine when I have the job fire every minute. Now I need to fire this job for a fixed number of times (specifically 2 times). How can I modify my code to do this?

@Bean
public SchedulerFactoryBean schedulerFactoryBeanJobOne() {
    schedulerFactoryBean = new SchedulerFactoryBean();
    schedulerFactoryBean.setAutoStartup(false);
    schedulerFactoryBean.setTriggers(procesoJobOneTrigger().getObject());
    schedulerFactoryBean.setJobDetails(procesJobOne().getObject());
    schedulerFactoryBean.setJobFactory(springBeanJobFactory());
    return schedulerFactoryBean;
}

@Bean
public SpringBeanJobFactory springBeanJobFactory() {
    return new AutowiringSpringBeanJobFactory();
}

@Bean
public JobDetailFactoryBean procesJobOne() {
    JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
    jobDetailFactory.setJobClass(JobOne.class);
    jobDetailFactory.setGroup("quartz");
    return jobDetailFactory;
}

@Bean
public CronTriggerFactoryBean procesoJobOneTrigger() {
    CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
    cronTriggerFactoryBean.setJobDetail(procesJobOne().getObject());
    // Runs every 60secs
    cronTriggerFactoryBean.setCronExpression("0/60 * * * * ?");
    cronTriggerFactoryBean.setGroup("quartz");
    return cronTriggerFactoryBean;
}

Upvotes: 2

Views: 290

Answers (1)

Mudassar
Mudassar

Reputation: 3205

Your cron expression is the place where you provide the no of times the job should run

 # * * * * *  command to execute
 # │ │ │ │ │
 # │ │ │ │ │
 # │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday)
 # │ │ │ └────────── month (1 - 12)
 # │ │ └─────────────── day of month (1 - 31)
 # │ └──────────────────── hour (0 - 23)
 # └───────────────────────── min (0 - 59)

you may use this:

# m   h   dom mon dow  
  0  14,15 *   *   *    

your job will be run at 14:00 and 15:00 i.e. twice

Upvotes: 2

Related Questions