spring-guides / gs-scheduling-tasks

Scheduling Tasks :: Learn how to schedule tasks with Spring.
https://spring.io/guides/gs/scheduling-tasks/
Apache License 2.0
132 stars 164 forks source link

Job running multiple times #9

Closed rmordillo-witbooking closed 8 years ago

rmordillo-witbooking commented 8 years ago

Hi!

I have scheduled 3 jobs :

@Slf4j
@Component
public class ScheduledTasks {

   @Autowired
   JobLauncher jobLauncher;

   @Autowired
   JobRegistry jobRegistry;

   // scheduled every 14 hours
   @Scheduled(cron = "* * */14 * * *")
   public void doPopulateCacheWithARIDataJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ populateCacheWithARIData ]");
      Job ariJob = jobRegistry.getJob("populateCacheWithARIData");

      // then
      jobLauncher.run(ariJob, new JobParametersBuilder().addString("time", LocalDateTime.now().toString()).toJobParameters());
   }

   // scheduled every 14 hours
   @Scheduled(cron = "* * */12 * * *")
   public void doMigrateBookingsJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ MigrateBookings ]");
      Job migrationJob = jobRegistry.getJob("migrateReservasToBookings");

      // then
      jobLauncher.run(migrationJob, new JobParametersBuilder().addString("time", LocalDateTime.now().toString()).toJobParameters());
   }

   // scheduled every 4 hours
   @Scheduled(cron = "* * */4 * * *")
   public void doPopulateCacheWithCalendarDataJob()
           throws NoSuchJobException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

      // given
      log.info("Running Scheduled task [ populateCacheWithCalendarData ]");
      Job calendarJob = jobRegistry.getJob("populateCacheWithCalendarData");

      // then
      jobLauncher.run(calendarJob, new JobParametersBuilder().addString("time", LocalDateTime.now().toString()).toJobParameters());
   }
}

And added @enableScheduling in my Spring boot's Application.java.

The thing is that the jobs run multiple times even after the first execution finishes with COMPLETED.

I don't know if this is due to some misconfiguration but can't fix it.

Any thoughts?

thanks in advance

gregturn commented 8 years ago

Have you tried @Scheduled(cron = "0 0 */4 * * *") as your expression for every 4 hours?

In the future, use markdown code fences to make things easier to read.

rmordillo-witbooking commented 8 years ago

Hi, I edited my post is fenced now , also edited the comment in java. It is supposed to be running every 4 hours. What I meant is that , for instance:

Job1 is launched Job1 is COMPLETED Just after this Job1 is launched again , and again

the same goes for the other 2 jobs.

Sorry I didn't explain it like this before.

gregturn commented 8 years ago

My suggestion is to alter your cron expression and use 0's in place of *'s as shown in the my comment.

rmordillo-witbooking commented 8 years ago

oh! sorry Let me try it this way then althought I thought * were used as a "any". I will keep you posted about it. thanks

gregturn commented 8 years ago

Well, * (in any programming context) means any. You are saying "any second, any minute", which kind of undermines trying to ONLY hit every x hours. But "0 0 */4" is like "on the 0th second and the 0th minute, and any hour divisible by 4, then go". If you have a whole slew of cron jobs that happen every 4 hours, it gives you the power to stagger them to different

4:00:00, 4:00:01, 4:00:02, 4:00:03, ... 4:01:00, 4:01:01, etc. would qualify if you used *'s like that.

rmordillo-witbooking commented 8 years ago

Hi! it seems that I understood or misread some tutorial about this topic. You were right, and now it works as I expected.

Thanks for your help

HemasekharT commented 5 years ago

I have some issues with this Jobs triggering from multiple instances of the Pivotal Cloud. How to prevent this. Any suggestions??