[英]Why is the @Async task in Spring Boot scheduler not running every 1 second
[英]Scheduler not running in Spring Boot
我創建了一個 Spring Boot 應用程序。 我已經配置了包含調度程序方法startService()
的類。 下面是我的代碼:
服務等級:
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()");
}
}
主要課程:
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);
}
}
我已將類注釋為@Component
,並將方法注釋為@Scheduled(fixedRate = 30000)
,它將作為調度程序運行。 但是在將應用程序作為 Spring Boot 運行時,調度程序不會觸發。 控制台顯示以下消息:
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'
誰能幫幫我。
也許你可以通過在配置文件中添加@ComponentScan注解來解決這個問題
@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);
}
}
我能夠解決問題,我忘記提供@service級別注釋,我已經為@Scheduler創建了檢查列表,請逐點檢查,它將幫助您解決問題。
隨意在評論中添加更多點,這將有助於解決問題。
一定是您忘記在您的應用程序類中添加@EnableScheduling 注解。
public static void main(String[] args) {
context = SpringApplication.run(YouApplication.class, args);
}
我終於能夠解決上述問題,我從包 com.mk.service 更改了我的服務類EnverseDemoService的package com.mk.service;
到com.mk.envers.service;
. 這是因為如果主配置類EnverseDemoApplication存在於包com.mk.envers
中。 引導應用程序中的所有其他類都應該在合格包中。 Eg: com.mk.envers.*;
就我而言,值為true
的lazy-initialization
阻止了 Spring 在啟動時加載我的@Component
並且@Scheduled
方法從未運行。
確保禁用 Spring Boot 延遲初始化。
請檢查 application.properties 中是否有“spring.main.lazy-initialization=true”
從 application.properties 中刪除它。
即使您的所有配置都正確,這條簡單的行也會啟用延遲加載,因為您的 @Component 將在應用程序啟動期間初始化。
正如 Swapnil 已經提到的所有檢查點,以確保在使用 cron 時。 您應該做的另一件事:始終使用以下參考站點驗證您的 cron 表達式是否格式正確 - http://www.freeformatter.com/cron-expression-generator-quartz.html#
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.