Reputation: 647
I have created a Spring Boot application. I have configured my class that contains the scheduler method startService()
.
Below is my code :
Service Class :
package com.mk.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.mk.envers.model.BossExtChange;
import com.mk.envers.model.BossExtChangeRepository;
@Component
public class EnverseDemoService {
@Autowired
BossExtChangeRepository bossExtChangeRepository;
@Scheduled(fixedRate = 30000)
public void startService() {
System.out.println("Calling startService()");
BossExtChange bossExtChange = bossExtChangeRepository.findById(5256868L);
System.out.println("bossExtChange.getDescription()--->"+bossExtChange.getDescription());
System.out.println("Ending startService()");
}
}
Main Class :
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@PropertySource("classpath:application.properties")
public class EnverseDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnverseDemoApplication.class, args);
}
}
I have annotated the class as @Component
and also method as @Scheduled(fixedRate = 30000)
that will running as a scheduler. But while running the application as Spring Boot the scheduler does not trigger. The console show the below message:
2016-02-03 10:56:47.708 INFO 10136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-02-03 10:56:47.721 INFO 10136 --- [ main] com.mk.envers.EnverseDemoApplication : Started EnverseDemoApplication in 3.231 seconds (JVM running for 3.623)
2016-02-03 10:56:47.721 INFO 10136 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@49e202ad: startup date [Wed Feb 03 10:56:44 IST 2016]; root of context hierarchy
2016-02-03 10:56:47.721 INFO 10136 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2016-02-03 10:56:47.736 INFO 10136 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
Can anyone please help me out.
Upvotes: 39
Views: 87448
Reputation: 169
In my case, 1 more scheduled task was already present(and running). If another scheduled task runs indefinitely, it might block the execution of other scheduled tasks. Creating the below bean solved my issue. I was using Spring Boot 3.6.0 and Java 17.
To increase the thread pool size, configure a custom task scheduler:
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10); // Adjust pool size as needed
scheduler.setThreadNamePrefix("ScheduledTask-");
return scheduler;
}
Upvotes: 0
Reputation: 1057
Just in case someone has this issue with an already existing Spring Boot application, I had a case that was really difficult to discover. My Scheduler was basically blocked by another scheduled method, so one scheduler was blocking the others without me knowing. I had to annotate the blocking scheduler with @Async
Upvotes: 3
Reputation: 3813
In my case I created @Component with class name name TaskExecutor, and spring boot just ignored this bean.
Upvotes: 0
Reputation: 11
Apart from things Swapnil mentioned in his answer (though not the case with OP), the method on which Scheduled annotation is placed must be a public class
Upvotes: 1
Reputation: 2859
In my case I had @PostConstruct
code in a loop which prevent spring context from finishing initialization, hence it didn't run scheduled methods.
Upvotes: 0
Reputation: 38
In my case I had an abstract @Service class containing the @Scheduled method, removing the abstract keyword it worked.
Upvotes: 0
Reputation: 5495
In my case I only add @Component
at the same class I used the @Scheduled
Upvotes: -1
Reputation: 21
Please check if in application.properties you have "spring.main.lazy-initialization=true"
Remove this from application.properties.
Even if you have all the configuration correct, this simple line will enable lazy loading due to which your @Component will initalize during application start.
Upvotes: 2
Reputation: 15
As Swapnil already mentioned all the check-points to make sure while using cron. One additional thing you should do: Always verify your cron expression whether it's in right format or not, using below reference site - http://www.freeformatter.com/cron-expression-generator-quartz.html#
Upvotes: 1
Reputation: 653
I was able to solve the issue, I forgot to provide @service level annotation, I have created the check List for @Scheduler, kindly go through every point one by one, It will help you to solve the issue.
Feel free to add more points in the comment so it will help to solve the issue.
Upvotes: 52
Reputation: 2295
In my case, was the lazy-initialization
with value true
which was preventing my @Component
to be loaded by Spring at startup and @Scheduled
method was never running.
Make sure the Spring Boot lazy initialization is disabled.
Upvotes: 7
Reputation: 4681
It must be that you forgot to add @EnableScheduling annotation in your app class.
public static void main(String[] args) {
context = SpringApplication.run(YouApplication.class, args);
}
Upvotes: 19
Reputation: 1251
May be you can solve this problem by adding the @ComponentScan annotation in the configuration file
@SpringBootApplication
@EnableScheduling
@ComponentScan(basePackages = "com.mk.service")
@PropertySource("classpath:application.properties")
public class EnverseDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EnverseDemoApplication.class, args);
}
}
Upvotes: 41
Reputation: 647
I was finally able to solve the above issue, I changed the package of my service class EnverseDemoService from package com.mk.service;
to com.mk.envers.service;
. This is because if the main configuration class EnverseDemoApplication is present in the package com.mk.envers
. All the other classes in the boot application should be in the qualifying package. Eg: com.mk.envers.*;
Upvotes: 15