Java 调度程序未在 Spring Boot 中运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35169161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:23:46  来源:igfitidea点击:

Scheduler not running in Spring Boot

javaspring-bootscheduler

提问by Mohith Kumar

I have created a Spring Boot application. I have configured my class that contains the scheduler method startService(). Below is my code :

我创建了一个 Spring Boot 应用程序。我已经配置了包含调度程序方法的类startService()。下面是我的代码:

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 @Componentand 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:

我已将类注释@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'

Can anyone please help me out.

任何人都可以帮助我。

采纳答案by Mohith Kumar

I was finally able to solve the above issue, I changed the package of my service class EnverseDemoServicefrom package com.mk.service;to com.mk.envers.service;. This is because if the main configuration class EnverseDemoApplicationis 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.*;

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

回答by Federico Traiman

May be you can solve this problem by adding the @ComponentScanannotation in the configuration file

也许你可以通过在配置文件中添加@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);
    }
}

回答by Pravin

It must be that you forgot to add @EnableScheduling annotation in your app class.

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

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