Kumar
Kumar

Reputation: 197

How to run a job from 20.35 until 23.35 every 30 minutes, some days a week?

I am facing a problem with cron expression. I have to ran a method from Thursday to Sunday by every 30 minutes. It time will start from 20:35 min till 23:35 min.

Cron expression:

"0 35/30 20-23 ? * THU-SUN";

As per my understanding; My method will invoke at 20:35 min at Thursday by every 30 minutes till Sunday.

My Expectation:

Method will invoke as per below timings:

Thu May 19 20:35:00 IST 2016
Thu May 19 21:05:00 IST 2016
Thu May 19 21:40:00 IST 2016

But; Method get invokes by below timings:

Thu May 19 20:35:00 IST 2016
Thu May 19 21:35:00 IST 2016
Thu May 19 22:35:00 IST 2016

Can anyone help me out. Why cron expression evaluating by every 1 hour.??

Here is code example:

@Scheduled(cron="0 35/30 20-23 ? * THU-SUN")
  public void startInboundSFTPChannel(){
      logger.info("Cron job started....");
      downloadSftpFilesController();
  }

Upvotes: 2

Views: 939

Answers (2)

Tomasz Dzieniak
Tomasz Dzieniak

Reputation: 2915

As I understand, your expression (0 35/30 20-23 ? * THU-SUN) means:

  • 0 - run at full minute only,
  • 35/30 - run every 30 minutes starts from 35,
  • 20-23 - run hours between 20 and 23,
  • ? - use implicit days from later part of expression,
  • * run at every single month,
  • THU-SUN - run at Thursday, Friday, Saturday and Sunday.

So, as you specified an increment instead of two values Quartz (which Spring uses) tries to calulate this like the following:

  • first, it calculates the value 35 for minutes - what matches 0-59 condition,
  • second, it adds 30 to previous 35 (which equals 65) what not matches 0-59 condition,
  • at the end, the only correct value is 35.

So, it runs your code every single hour when minutes == 35.

Can you handle running the code one more time at Thu May 19 20:05:00 IST 2016?

If yes, then you can use one of the following expressions:

  1. 0 5,35 20-23 ? * THU-SUN

Which means:

  • 0 - run at full minute only,
  • 5,35 - run every 30 minutes, starting from minutes == 5,
  • 20-23 - run hours between 20 and 23,
  • ? - use implicit days from later part of expression,
  • * - run at every single month,
  • THU-SUN - run at Thursday, Friday, Saturday and Sunday.

    1. 0 5/30 20-23 ? * THU-SUN

Which means:

  • 0 - run at full minute only,
  • 5/30 - run every 30 minutes starting from minutes == 5,
  • 20-23 - run hours between 20 and 23,
  • ? - use implicit days from later part of expression,
  • * - run at every single month,
  • THU-SUN - run at Thursday, Friday, Saturday and Sunday.

Here you can find similar problem.

Upvotes: 2

fedorqui
fedorqui

Reputation: 290515

If you want the command to run from 20.35 to 23.35 every day, from Thursday to Sunday, you can define it in two steps:

35   20    ? * THU-SUN
5-59 21-23 ? * THU-SUN

There is no easy way to set this up in just a cron expression, because you don't want it to run at 20.05.

That is: at 20 , run at the minute 35. At 21 to 23 h, every 30 minutes with an offset of 5 minutes.


I based my answer on this format:

 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
 *  *  *  *  *  command to be executed 

Upvotes: 2

Related Questions