Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between * and ? in Spring @Scheduled(cron=".....")

I've been looking at the Spring Boot example for scheduling tasks (https://spring.io/guides/gs/scheduling-tasks/) and reading through some documentation (https://javahunter.wordpress.com/2011/05/05/cronscheduler-in-spring/) and I see that * and ? are used almost interchangeably.

For example, the line

@Scheduled(cron = "0 15 10 ? * *") 

and

@Scheduled(cron = "0 15 10 * * ?") 

do the exact same thing. So what is the difference between * and ?

like image 359
Jordan Avatar asked May 20 '15 05:05

Jordan


People also ask

What is @scheduled in spring?

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.

Is cron expression 5 or 6 fields?

A cron expression is a string consisting of six or seven subexpressions (fields) that describe individual details of the schedule. These fields, separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field.

What is cron expression in spring?

Cron expressions enable us to schedule tasks to run periodically at a specific date and time. After its introduction in Unix, other Unix-based operating systems and software libraries (including the Spring Framework) adopted its approach for task scheduling.

Is @scheduled asynchronous?

There is no need to use @Async. Just use fixedRate attribute of @Scheduled instead of fixedDelay. Spring will make another invocation on the method after the given time regardless of any call is already being processed.


2 Answers

The tutorial is outdated. The symbol ? means exactly the same as the symbol *.

As of Spring version 3.1.2.RELEASE, the call hierarchy is the following:

  1. The constructor CronTrigger(String) calls the constructor CronSequenceGenerator(String)
  2. CronSequenceGenerator(String) calls parse(String)
  3. parse(String) calls setDays(BitSet bits, String field, int max).

Its implementation is clear:

private void setDays(BitSet bits, String field, int max) {     if (field.contains("?")) {         field = "*";     }     setNumberHits(bits, field, 0, max); } 

So, if ?, then *.

like image 71
Luchostein Avatar answered Sep 24 '22 09:09

Luchostein


asterix stands for all possible values. question marks should be used for non specific value

*("all values") - used to select all values within a field. For example, "" in the minute field means *"every minute".

? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification.

Copied from the tutorial

like image 31
Jens Avatar answered Sep 26 '22 09:09

Jens