簡體   English   中英

調度程序未在 Spring Boot 中運行

[英]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創建了檢查列表,請逐點檢查,它將幫助您解決問題。

  1. 檢查 SpringBoot Main 類上的 @EnableScheduling。
  2. 計划方法應使用@Scheduled 注解,遵循@Scheduled 方法規則。 一個方法應該有 void 返回類型,一個方法不應該接受任何參數。
  3. 確保該類應使用 @Service 或 @Component Annotation 進行注釋,以便 SpringBoot 可以生成該類的對象。
  4. 調度程序作業的包應該在主應用程序類的包下。 例如 com.company 是您的主要應用程序類包,那么調度程序類包應該是 com.company.scheduler,
  5. 如果您使用 Cron 表達式,請確認相同,例如 @Scheduled( cron = "0 0/2 * * * ?"),此 Cron 表達式將每 2 分鍾安排一次任務。

隨意在評論中添加更多點,這將有助於解決問題。

一定是您忘記在您的應用程序類中添加@EnableScheduling 注解。

public static void main(String[] args) {
        context = SpringApplication.run(YouApplication.class, args);
    }

我終於能夠解決上述問題,我從包 com.mk.service 更改了我的服務類EnverseDemoServicepackage com.mk.service; com.mk.envers.service; . 這是因為如果主配置類EnverseDemoApplication存在於包com.mk.envers中。 引導應用程序中的所有其他類都應該在合格包中。 Eg: com.mk.envers.*;

就我而言,值為truelazy-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM