1

I wrote a scheduler

@Documented
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Scheduled(cron="0 0 0 * * *")
public @interface DeleteAndCopy{

}

What means every midnight. The process takes about 4 hours. (0am-4am)

Now there is a maintainance day on 28.07.2017 at 1:00 pm.

I decided to not copy data at this date to not have an unexpected state. How to exclude this date from the execution?

Grim
  • 1,938
  • 10
  • 56
  • 123
  • @Zeromus Is there something like an @NonScheduled-Annotation? Or a Incode-suggestion? – Grim May 09 '17 at 07:52

3 Answers3

1

Cron doesnt support exclusion. Fastest i could come up with is a double @scheduled annotation like this

@Scheduled(cron = "0 0 0 * 1-6,8-12 *")  //every month except 7
@Scheduled(cron = "0 0 0 1-27,29-31 7 *") //every day of month 7 except 28

(tried first with minutes with something like this and it seems to work for me

@Scheduled(cron = "00 1-20,25-59 * * * *") //every minute except 21,22,23,24 for every hour
@Scheduled(cron = "00 21 10 * * *") //minute 21 for 10 am (dunno your timezone)

)

Definitely not elegant though

Zeromus
  • 4,472
  • 8
  • 32
  • 40
0

The cron syntax doesn't seem to support exclusion of a specific date. See this answer from the Unix and Linux StackExchange site:

Standard cron syntax is quite simple, it does not support exclusions. In some cases it is possible to create a list of several/many cron entries to implement such logic, but that tends be tedious and hard to understand or maintain; this approach is not applicable in your case though (not least because standard cron has no notion of the calendar year).

I can think about a couple of a few solutions for your problem:

  • Make the logic executed by the timer conditional, as described in this answer. You could have a boolean property which determines whether the logic should execute or not. The timer will unfortunately still trigger, but you can use the property to not execute its logic.
  • A little more sophisticated solution would be the same as above, but keep a list of dates for which to disable the execution.
  • Even simpler, but much less sophisticated, add the exclusion of the date directly to the code: if (!LocalDate.now().equals(LocalDate.of(2017, 7, 28))). Depending on the type of application, this might be acceptable.
  • A fourth option would be to programatically create the scheduler. You could use HolidayCalendar from the Quartz API to exclude the date. It has a method addExcludedDate(Date date) which does exactly what you want. A full example, using Spring, is available in this preview of an E-book. I am not sure if I am allow to copy the example, so I am only posting the link.
Community
  • 1
  • 1
Magnilex
  • 11,584
  • 9
  • 62
  • 84
0

By principle a CMMS can be asked for planed Maintainance scopes in-code.

Grim
  • 1,938
  • 10
  • 56
  • 123