I'm working with #Scheduled annotations. I need to run the method in different moments, exactly each 2 and 5 seconds.
How can I do it?
In this moment my code is the next:
#Scheduled(cron = "${cron.startdate}")
public void check() {
LOGGER.info("1 - Check DB");
}
and the application.yml:
cron:
startdate: 0/2 * * * * *
My configuration will be executed each 2 seconds, but I need it to be executed each 5 seconds too.
The output should be:
[11:20:00] | 1 - Check DB
[11:20:02] | 1 - Check DB
[11:20:04] | 1 - Check DB
[11:20:05] | 1 - Check DB
[11:20:06] | 1 - Check DB
[11:20:08] | 1 - Check DB
[11:20:10] | 1 - Check DB
[11:20:12] | 1 - Check DB
.
.
.
Thanks guys.
I think #Scheduled(cron = "0/2,0/5 * * * * *") should work.
Related
I have a requirement where my scheduler has to run for every hour between 9 AM to 12 PM everday and then it runs once by 3PM and 5 PM respectively.
How can i make my CRON expression to work according to the requirement
So far i have tried with below cron expression
#Scheduled(cron="0 0 9-12 * * *")
But that runs for every hour from 9 AM - 12 PM
How can i modify it to make it run once by 3 PM and 5 PM also.
I have tried by combining two cron expressions like below but that dint work.
#Scheduled(cron="0 0 9-12 * * *, 0 0 3 * * *")
Can i give 2 cron expressions through 2 #Scheduled like below
#Scheduled(cron="0 0 9-12 * * *")
#Scheduled(cron="0 0 9-12 * * *")
public void myMethod() {}
Would that be a feasible solution?
Can anybody help me with this?
#Schedules annotation can be used to set multiple calendar-based timer expressions.
This annotation will get rid of the redundancy of writing the same
method multiple times.
In the following example, the first expression sets a timer to expire on the last day of every month. The second expression sets a timer to expire every Friday at 11:00 PM.
#Schedules ({
#Schedule(dayOfMonth="Last"),
#Schedule(dayOfWeek="Fri", hour="23")
})
public void doPeriodicCleanup() { ... }
I have this code.
This cron message means "do this method every Sunday in 01.00 a.m." or I make a mistake translating this?
#Scheduled(cron = "0 1 0 * * ?")
private void notificationsScheduler() {
//implementation
}
you are wrong, it means every day.
Your expression
"0 1 0 * * ?"
means: At 00:01:00am every day
As per your requirement : At 01:00:00am, on every Sunday, every month
Use:
0 0 1 ? * SUN *
Follow this https://www.freeformatter.com/cron-expression-generator-quartz.html for more detail.
I have a Spring app with this property:
cobra.tarifas.intervaloEntreCobrancas =*/37 * * * * *
And this is where it's used:
#Scheduled(cron = "${cobra.tarifas.intervaloEntreCobrancas}")
public void cobraTarifaDMaisZero() {
int number = new Random().nextInt();
System.out.println("started " + number + " at " + LocalTime.now().withNano(0));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished " + number + " at " + LocalTime.now().withNano(0));
}
So every time it runs I have a log that points out when it started and finished, along with a "unique" identifier (Spring uses same instance at #Scheduled classes with no extra configuration, this.toString returned same string everytime). Every execution takes 5 seconds to complete.
I thought that cron expression meant "runs everytime every 37 seconds" but it didn't.
Using * * * * * * I got this:
started -1615036471 at 10:18:46
finished -1615036471 at 10:18:51
started 2090620070 at 10:18:52
finished 2090620070 at 10:18:57
started -349207943 at 10:18:58
finished -349207943 at 10:19:03
Which made sense: it takes 1s to start a new execution after a previous one was finished, always taking 5s to complete. But when I used */37 * * * * * I got
started -644623959 at 10:54
finished -644623959 at 10:54:05
started 212117957 at 10:54:37
finished 212117957 at 10:54:42
started 1788724609 at 10:55
finished 1788724609 at 10:55:05
started 362510867 at 10:55:37
finished 362510867 at 10:55:42
started -25103618 at 10:56
finished -25103618 at 10:56:05
started -820939074 at 10:56:37
finished -820939074 at 10:56:42
Why it's starting only at seconds 00 and 37? I wanted to implement a similar behavior as Spring's #fixedDelay but flexible to be changed at some properties file(#fixedDelay only accept constants).
I don't think that you can actually do this only with cron, for a number such as 37.
The easiest option, would be to select an increment that can divide 60.
Basically an even number that is equal to ( or less than) 30.
E.g. So 10, 15, 20, or 30.
For 10, your expression would be:
cobra.tarifas.intervaloEntreCobrancas =0,10,20,30,40,50 * * * * *
Otherwise, you could just go for once per minute.
0 * * * * *
Can anyone suggest a way to set a Cron trigger to run every 1 hour?
I tried to do something like this:
#Scheduled(cron = "0 0 0/1 1/1 * ? *")
but when I run the server I get Error :
Error 404--Not Found
What is wrong in this cron?
Try any one of these:
0 0 0/1 * * ?
0 0 * * * ?
This means 0 seconds 0 minutes on change of every hour.
I've used the following cron statement:
#Scheduled(cron = "59 * * * * *")
I have following spring job to run every day two times please check my following cron express is that correct to trigger every day two times.
#Scheduled(cron = "0 0 24/12 * ? *")
public void demoService()
{
}
I tried the above expression, but this didn't work. What's wrong here?
You could use
0 0 0/12 * * ?
which means every 12 hours.
#Scheduled(cron = "0 0 0/12 * * ?")
public void demoService()
{
}
Hope it could help.