#Scheduled is running twice with spring 4 java configuration - java

I am using Spring 4.0.2 with Java configuration. There is not an XML configuration.
I have #Scheduled annotation for a cron job. It is running twice. Can someone help me? This is what I'm trying.
#Scheduled(cron = "1 * * * * ?"

#Scheduled(cron = "1 * * * * ?") : Runs your job at 01 second of every (any) minute.
So you job runs once every minute.
Test:
#Scheduled(cron = "1 * * * * ?")
public void run(){
System.out.println("Running Test Run : "+DateTime.now());
}
Output:
Running Test Run : 2016-06-08T20:06:01.019Z
Running Test Run : 2016-06-08T20:07:01.015Z
Running Test Run : 2016-06-08T20:08:01.013Z
Running Test Run : 2016-06-08T20:09:01.011Z
More info on Spring cron expressions.

Related

Issue with cron expression in java spring boot application

I am running a job scheduler in spring boot application.
To execute job every 5 min, I am using below cron expression
#Scheduled(cron = "0 0/5 * * * *", zone ="EST").
This is working fine
But when I try to execute the same job at a specific time, for instance at 11:30 AM, I am using below cron expression
#Scheduled(cron = "0 30 11 * * *", zone ="EST")
This is not working
I couldn't find any mistake w.r.t syntax. Please help if I am doing anything wrong

Start a scheduled spring batch job 5 seonds after start of previous one

I have a spring batch job with a #Scheduled(fixedDelay=5000) annotation. But it starts 5 seconds after the end of the previous execution. How can I start it 5 seconds after the start of the previous execution?
Use #Scheduled(fixedRate = 5000). You can also use a CRON expression #Scheduled(cron = "*/5 * * * * ?") but it seems like an overkill.

Spring scheduler cron expression not working

We are use spring scheduler with following setting but wondering why its not working for us?
Our expectation is it should execute every day at 2 AM, Is anything wrong with that?
<task:scheduled ref="invoiceScheduler" method="updateInvoiceStatusToOverDue" cron="0 0 2 * * ?" />
Thanks in Advance.
This cron works for my SpringBoot application:
#Scheduled(cron = "0 0 2 1/1 * *")
Btw, what "doesn't work"? Could you please elaborate?
The following task is being scheduled to run 10 minutes past each hour but only during the 8-to-5 "business hours" on weekdays.
scheduler.schedule(task, new CronTrigger("0 10 8-17 * * MON-FRI"));
Could you please try this:
scheduler.schedule(task, new CronTrigger("0 1 2 * * MON-FRI"));
OR
scheduler.schedule(task, new CronTrigger("0 1 2 * * *"));
"0 0 2 * * *" instead of "0 0 2 * * ?" helped us to achieve scheduler to invoke every day at 2 AM. Thanks everyone for finding time and helping me.

Spring execute method every 15 minutes

I tried to use cron expression from this site http://www.cronmaker.com/
#Scheduled(cron = "0 0/15 * 1/1 * ? *")
public void clearRps() {
}
But it throws: java.lang.IllegalStateException: Encountered invalid #Scheduled method 'clearRps': Cron expression must consist of 6 fields (found 7 in "0 0/15 * 1/1 * ? *")
Just use the following cron:
#Scheduled(cron = "0 0/15 * * * *")
Spring cron expression syntax slightly differs from unix cron expression. One immediate difference - it supports 1 less field (6 rather than 7).

How to use #Scheduled(cron) with SpEL in spring?

I have a method that I want spring to schedule - for that matter I'm using the #Scheduled annotation - and to be more exact, I'm using a cron expression.
My cron expression is in a property file that is called scheduler.properties.
When I'm using it as a placeholder #Scheduled(cron="${cron}") - everything works great; but I want to use SpEL ( #Scheduled(cron="#{scheduler['cron']}") ) , and it does't work - throws the following exception:java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in #{scheduler['cron']})
What am I doing wrong here?
EDIT:
Here is my cron expression from the properties file: cron=0 0/1 * * * ?
Here is the stack trace that I get:
java.lang.IllegalArgumentException: cron expression must consist of 6 fields (found 1 in #{scheduler['cron']})
at org.springframework.scheduling.support.CronSequenceGenerator.parse(CronSequenceGenerator.java:233)
at org.springframework.scheduling.support.CronSequenceGenerator.<init>(CronSequenceGenerator.java:81)
at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:54)
at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:44)
at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:188)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:209)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:1)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
SECOND EDIT:
It seems that spring is trying to parse the following string as the cron experssion "#{scheduler['cron']}" insraed of the actual cron expression itself.
According to the error message, the value of the cron expression in your properties file is incorrect.
It does not conform to the expected syntax.
The value should contain six fields and look something like this.
* 10 * * * *
Here's the code that throws this exception
/**
* Parse the given pattern expression.
*/
private void parse(String expression) throws IllegalArgumentException {
String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
if (fields.length != 6) {
throw new IllegalArgumentException(String.format(""
+ "cron expression must consist of 6 fields (found %d in %s)", fields.length, expression));
}
It may not be possible to externalize cron configuration using spEL in an Annotation.
The alternatives are to use XML or use the cron expression.
http://forum.springsource.org/showthread.php?91203-Scheduled-and-externalization-of-configuration-for-fixedDelay-and-fixedRate-problem
Always specify like this in property file: Notice the space in between frequency.
run refresh job every day a 9am
job.cron.rate=0 0 9 * * *
Example patterns:
* "0 0 * * * *" = the top of every hour of every day.
* "*/10 * * * * *" = every ten seconds.
* "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
* "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
* "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
* "0 0 0 25 12 ?" = every Christmas Day at midnight
Use it like this in Code:
#Scheduled(cron = "${job.cron.rate}")
public void perform() throws InterruptedException {
}
I had a similar issue and resolved it by reading property file with context:property-placeholder
<util:properties id="applicationProps" location="/WEB-INF/classes/properties/application.properties" />
**<context:property-placeholder properties-ref="applicationProps" />**
Hope it helps someone!!
It works. I spent days figuring out... but this indeed works.
You should set environment variable like you do for JAVA_HOME etc.
Close your IDE.
export cron_scheduler_expression="0 19 21 * * *"
Then restart your IDE, Eclipse or NetBeans whatever you are using.
#Scheduled(cron = "${cron_scheduler_expression}")
public void runSchedulerTask(){
}

Categories

Resources