Gal Sosin
Gal Sosin

Reputation: 734

Schedule a job every hour between 50 and 60 minutes once

I'm trying to use @Schedule in spring with and I would like to know if there is an option to use cron to run every hour once between minute 50 and 59, for example: it will run: 13:58, 14:52, 15:57 16:50 etc...

thanks

Upvotes: 0

Views: 1826

Answers (3)

dpr
dpr

Reputation: 10972

How about

0 55 * 1/1 * ? *

cronmaker.com always was a good resource to me for generating cron expressions.

I think @Scheduled only accepts static cron expressions and you must not use SpEL in the expression.

You can however implement a custom Trigger. Together with the to be executed task, this trigger can be registered by implementing SchedulingConfigurer in your @Configuration class using ScheduledTaskRegistrar.addTriggerTask.

Upvotes: 0

Ilias Mentz
Ilias Mentz

Reputation: 520

See this example:

0 50-59/1 * * *

From the crontab man page, section 5:

       field         allowed values
       -----         --------------
       minute        0-59
       hour          0-23
       day of month  1-31
       month         1-12 (or names, see below)
       day of week   0-7 (0 or 7 is Sun, or use names)

 A field may be an asterisk (*), which always stands for ``first-last''.

 Ranges of numbers are allowed.  Ranges are two numbers separated with a hyphen.  The specified range is inclusive.  For example, 8-11 for an
 ``hours'' entry specifies execution at hours 8, 9, 10 and 11.

 Lists are allowed.  A list is a set of numbers (or ranges) separated by commas.  Examples: ``1,2,5,9'', ``0-4,8-12''.

Upvotes: 1

Maxim Kasyanov
Maxim Kasyanov

Reputation: 1058

You could use RandomValuePropertySource from Spring

Try it:

@Scheduled(cron='0 ${random.int[0,9]} * 1/1 * *')

Upvotes: 0

Related Questions