I have a simple test method here that is set to run every 5 seconds and it does, but looking at the System.out you can see it appears to be doing something odd.
i am using spring 4.0.3 and i have this problem. i solved it by renaming my beans.
to:
<task:annotation-driven executor="taskExecutor"
scheduler="taskScheduler" />
<task:executor id="taskExecutor" pool-size="25" />
<task:scheduler id="taskScheduler" pool-size="25" />
I noticed some INFO logging saying that no bean named taskScheduler found, creating a new instance. So I figured there were two instance of the taskScheduler.
Let me know if this works for you too :)
I had the same problem. Spent hours trying to resolve.
Solution was the application was deploying twice on Tomcat.
When trying to clean Tomcat it gave an error.
Checking the server.xml Tomcat file I noticed the same was being deployed twice. There was also an unclosed "host" tag. Not sure which of these fixed it but relieved to have it working properly again.
I encountered similar problem. It could be because of below reasons.
A bug in spring versions https://jira.spring.io/browse/SPR-10830
Context being loaded twice.
The log4j.xml writing the logs twice. It happened in my case not sure about yours. If you have tried the other options, try this one also.
According to this post and Spring Jira this is a bug in Spring Framework Scheduler component.
I had the same problem. I was using annotation based configuration as follows:
@Configuration
@EnableScheduling
public class SchedulerConfig {
@Scheduled(fixedDelay = 60 * 1000)
public void scheduledJob() {
// this method gets called twice...
}
}
I was also extending AbstractAnnotationConfigDispatcherServletInitializer for initializing the same.
public class SpringWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { MainConfig.class, SchedulerConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {SpringWebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected Filter[] getServletFilters() {
final CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding(CHARACTER_ENCODING);
encodingFilter.setForceEncoding(true);
return new Filter[] { encodingFilter };
}
}
Removing the SchedulerConfig.class
from getRootConfigClasses()
method did the trick for me.
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { MainConfig.class };
}
I hope this helps.
In the application.properties, add the following property which tells the Spring Boot Application to not start the Batch Job on Application startup.
spring.batch.job.enabled=false