Spring scheduled cron job running too many times - spring-boot

My job is running every at the time specified but its running every second of time specified, for example if I set a job to run at 22:54 it will run every second from 22:54:00 until 22:54:59. I want it to just run once at the time specified...any help is much appreciated
my code:
#Scheduled(cron = "* 54 22 * * ?")
public void getCompaniess() {
System.out.println(new Date()+" > Running testScheduledMethod...");
}
output:
Thu Mar 12 22:54:00 GMT 2020 > Running testScheduledMethod...
Thu Mar 12 22:54:01 GMT 2020 > Running testScheduledMethod...
.....
Thu Mar 12 22:54:59 GMT 2020 > Running testScheduledMethod...

Change the first * to 0, with a star you are saying "every second".
Replacing it with a 0 (or any other number 0-59) will have it run on that "second" instead of "all of them".

#Scheduled(cron = "0 54 22 * * ?")
public void getCompaniess() {
System.out.println(new Date()+" > Running testScheduledMethod...");
}

Related

AWS Cron expression error: "Parameter ScheduleExpression is not valid."

I am trying to run a lambda 18 pm UTC every Friday:
new Rule (this, 'lambda', {
schedule: Schedule.expression('cron(0 18 ? * FRI *'),
targets: [lambdaTarget],
})
But received error "Parameter ScheduleExpression is not valid."
What is wrong with the expression?
Assuming it isn't a typo in your question.
schedule: Schedule.expression('cron(0 18 ? * FRI *'),
should be
schedule: Schedule.expression('cron(0 18 ? * FRI *)'),
Notice the extra ')'.

Meaning of cron expression 0 * * * * *

I want to know about the meaning of this expression in
0 * * * * *
I think it means the scheduler is expected to run every seconds.Can anyone confirm me about this?
#Scheduled(cron = "0 * * * * *")
To be more precise , you can use CronSequenceGenerator to verify the execution time of a cron expression . Spring internally use this object to calculate the next triggered time of a cron expression.
For example, the following function will simply print out the next 10 triggered time.
public static void printNextTriggerTime(String cronExpression, LocalDateTime currentTime) {
CronSequenceGenerator generator = new CronSequenceGenerator(cronExpression);
Date d = Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant());
for (int i = 0; i < 10; i++) {
d = generator.next(d);
System.out.println(d);
}
}
So , if I input :
printNextTriggerTime("0 * * * * *", LocalDateTime.of(2019, 8, 20, 15, 30, 0));
It will output :
Tue Aug 20 15:31:00 HKT 2019
Tue Aug 20 15:32:00 HKT 2019
Tue Aug 20 15:33:00 HKT 2019
Tue Aug 20 15:34:00 HKT 2019
Tue Aug 20 15:35:00 HKT 2019
Tue Aug 20 15:36:00 HKT 2019
Tue Aug 20 15:37:00 HKT 2019
Tue Aug 20 15:38:00 HKT 2019
Tue Aug 20 15:39:00 HKT 2019
Tue Aug 20 15:40:00 HKT 2019
which means 0 * * * * * will run at every minute but not second.
An update on the answer by Ken Chan taking into account that CronSequenceGenerator is deprecated in Spring in favour of CronExpression:
public static void printNextTime(String cronString) {
CronExpression exp = CronExpression.parse(cronString);
Stream.iterate(now(), exp::next).limit(10).forEach(System.out::println);
}
The call printNextTime("0 * * * * *"); generates:
2022-12-12T08:56:38.442809
2022-12-12T08:57
2022-12-12T08:58
2022-12-12T08:59
2022-12-12T09:00
2022-12-12T09:01
2022-12-12T09:02
2022-12-12T09:03
2022-12-12T09:04
2022-12-12T09:05

Spring Cron Expression to run every Tuesday night 9?

I am using Spring schedule. I configured the following Cron expression to run my task every Tuesday night at 9pm,
"0 0 21 * * TUE"
However, I am getting the following exception when am starting the application
Encountered invalid #Scheduled method 'runSchduler': Cron expression must consist of 6 fields
Is my Spring Cron expression wrong?
Is my Spring Cron expression configured, to run every Tuesday night at 9
wrong?
Yes :)
But try,
0 0 21 ? * TUE
Or with the Spring annotation:
#Scheduled(cron = "0 0 21 * * TUE")
The following is a really handy website for creating Cron expressions.
http://www.cronmaker.com/
Take note: Just remove the last element from the created expression to use it with Spring scheduling.
And a nice way to verify it in Natural Language here
Cron Expression for Everyday Tuesday Midnight 9 PM
0 0 21 ? * TUE
cleck below cron with zone example
#Component
public class SpringScheduling {
#Scheduled(cron = "0 0 21 ? * TUE",zone="Asia/Calcutta")
public void trackScheduling() {
System.out.println("Scheduled task running");
}
}
Looks like you have a field too much?
Just generated this based on your criteria of Tuesdays at 9PM
"0 21 * * 2"

How do I create a cron expression running in Kibana on weekday?

I would like my watcher to run from Monday to Friday only. So I'm trying to use this schedule:
"trigger": {
"schedule" : { "cron" : "0 0 0/4 * * MON-FRI" }
},
"input": {
...
However, I'm getting
Error
Watcher: [parse_exception] could not parse [cron] schedule
when I'm trying to save the watcher. Removing MON-FRI does helps but I need it.
This expression works:
0 0 0/4 ? * MON-FRI
But I'm not sure I understand why ? is required for either the day_of_week or day_of_month
Thank you!
I believe this is what you are looking for:
"0 0 0/4 ? * MON-FRI"
You can use croneval to check your cron expressions 1:
$ /usr/share/elasticsearch/bin/x-pack/croneval "0 0 0/4 ? * MON-FRI"
Valid!
Now is [Mon, 20 Aug 2018 13:32:26]
Here are the next 10 times this cron expression will trigger:
1. Mon, 20 Aug 2018 09:00:00
2. Mon, 20 Aug 2018 13:00:00
3. Mon, 20 Aug 2018 17:00:00
4. Mon, 20 Aug 2018 21:00:00
5. Tue, 21 Aug 2018 01:00:00
6. Tue, 21 Aug 2018 05:00:00
7. Tue, 21 Aug 2018 09:00:00
8. Tue, 21 Aug 2018 13:00:00
9. Tue, 21 Aug 2018 17:00:00
10. Tue, 21 Aug 2018 21:00:00
For the first expression you'll get following java exception:
java.lang.IllegalArgumentException: support for specifying both a day-of-week AND a day-of-month parameter is not implemented.
You can also use Crontab guru to get human readable descriptions like:
At every minute past every 4th hour from 0 through 23 on every day-of-week from Monday through Friday.
The question mark means 'No Specific value'. From the documentation on Quartz's website:
? (“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.
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
I suppose since you want your schedule to run every 4 hours, mon-fri, the actual day of the month is irrelevant, so the ? specifies that. * on teh other hand would be 'all values' which would not make sense since you are specifying only mon-fri for day of the week.
Hope that helps!

Run a cron job every 5 minute only on specific hours?

I want to run some cronjob between monday-friday at 23.50 and 23.55. I tried some expressions but there is something wrong with my expression.
0 50/5 23-00 ? * MON-FRI
When I calculate next days with cronmaker it shows;
1. Tuesday, March 15, 2016 11:50 PM
2. Tuesday, March 15, 2016 11:55 PM
3. Wednesday, March 16, 2016 12:50 AM
4. Wednesday, March 16, 2016 12:55 AM
5. Wednesday, March 16, 2016 11:50 PM
Why it shows AM? I only specified 23-00 hours?
It should be 0 50/5 23 ? * MON-FRI
'-' specifies range. So, 0 50/5 23-00 ? * MON-FRI means 11pm to 12am
12:50 AM is the same as 00:50 or 24:50. 50 minutes after midnight. Since the hour is 00 and the minute 50 it matches your cron expression.
0 50/5 23-00 ? * MON-FRI
it can be write as below may fullfill your requirement
0 50/5 23 * * 1-5 ?
explain 1-5 means 1 = monday
... 5 = Friday
note: sunday = 0 or 7

Resources