2

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();
  }
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Kumar
  • 197
  • 3
  • 12

2 Answers2

2

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.

Community
  • 1
  • 1
Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
2

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 
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • got you. Thanks. If I start from 20:35 min and add 30 min in time then its addition will be 65. which will be greater that (0-59). That's why my cron expression is not evaluating properly. – Kumar May 19 '16 at 11:42
  • @Kumar yes, exactly. You can always evaluate your cron expressions in http://crontab.guru/ – fedorqui May 19 '16 at 12:16
  • Thanks buddy.. @fedorqui – Kumar May 20 '16 at 10:57
  • Yep @fedorqui. I have to modify cron expression. By which minutes should not exceed than 59. – Kumar May 20 '16 at 11:29