调度程序未在Spring Boot中运行

Moh*_*mar 11 java scheduler 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()");
    }
}
Run Code Online (Sandbox Code Playgroud)

主类:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经注释了类@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'
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我.

Fed*_*man 21

可能是您可以通过在配置文件中添加@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);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 14

一定是您忘记在您的应用程序类中添加 @EnableScheduling 注释。

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

  • 虽然问题中存在“@EnableScheduling”,但这个答案仍然有助于强调这一点。 (2认同)

Moh*_*mar 9

我终于能够解决上述问题,我改变了我的服务类的包EnverseDemoServicepackage com.mk.service;com.mk.envers.service;.这是因为如果主要配置类EnverseDemoApplication存在于包中com.mk.envers.引导应用程序中的所有其他类应该在合格包中.Eg: com.mk.envers.*;


San*_*bat 7

就我而言,lazy-initializationwith 值true阻止了@ComponentSpring 在启动时加载我的方法,并且@Scheduled方法从未运行。

确保 Spring Boot延迟初始化已禁用。


小智 7

我能解决问题,我忘记提供@service level annotation,我已经为@Scheduler创建了检查列表,请一一检查每一点,它会帮助您解决问题。

  1. 检查 SpringBoot Main 类上的 @EnableScheduling。
  2. 调度方法应使用@Scheduled 进行注释,遵循@Scheduled Method 规则。一个方法应该有 void 返回类型,一个方法不应该接受任何参数。
  3. 确保该类应使用@Service 或@Component Annotation 进行注释,以便 SpringBoot 可以创建该类的对象。
  4. 调度程序作业的包应该在主应用程序类的包下。例如 com.company 是您的主要应用程序类包,那么调度程序类包应该是 com.company.scheduler,
  5. 如果您使用 Cron 表达式确认相同,例如 @Scheduled( cron = "0 0/2 * * * ?"),此 Cron 表达式将每 2 分钟安排一次任务。

随意在评论中添加更多点,以便它有助​​于解决问题。

  • 谢谢!检查“@EnableScheduling”对我有帮助:我没有将它添加到我的应用程序主类中...... (2认同)