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.
The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. The @Scheduled annotation is used to trigger the scheduler for a specific time period.
How do I test @Scheduled job tasks in my spring-boot application? What do you want to test exactly? If you want to test that work() does what it's supposed to do, you can test it like any other method of any other bean: you create an instance of the bean, call the method, and test that it does what it's supposed to do.
Canceling the Scheduled Future. Another way to stop the scheduler would be manually canceling its Future. In the cases with multiple scheduler tasks, then we can maintain the Future map inside of the custom scheduler pool but cancel the corresponding scheduled Future based on scheduler class.
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With