Spring 启动中的调度程序未 运行
Scheduler not running in Spring Boot
我已经创建了一个 Spring 引导应用程序。我已经配置了包含调度程序方法 startService()
的 class。
下面是我的代码:
服务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()");
}
}
主要 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);
}
}
我已将 class 注释为 @Component
,并将方法注释为 @Scheduled(fixedRate = 30000)
,它将 运行 作为调度程序。但是当 运行 应用程序作为 Spring 启动时,调度程序不会触发。控制台显示以下消息:
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'
谁能帮帮我。
我终于能够解决上述问题,我将我的服务包 class EnverseDemoService 从 package com.mk.service;
更改为 com.mk.envers.service;
.这是因为如果主要配置 class EnverseDemoApplication 存在于包 com.mk.envers
中。引导应用程序中的所有其他 classes 应该在合格包中。 Eg: com.mk.envers.*;
也许你可以通过在配置文件中添加@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);
}
}
一定是你忘记在应用中添加@EnableScheduling 注解了class。
public static void main(String[] args) {
context = SpringApplication.run(YouApplication.class, args);
}
在我的例子中,是 lazy-initialization
的值 true
阻止了我的 @Component
在启动时被 Spring 和 @Scheduled
方法加载从来没有 运行.
确保 Spring 启动 lazy initialization 被禁用。
我能够解决这个问题,我忘记提供@service 级别注释,
我已经为@Scheduler 创建了检查列表,请逐一检查每个点,它会帮助您解决问题。
- 检查 SpringBoot Main 上的@EnableScheduling class。
- Scheduled方法需要注解@Scheduled,遵循@Scheduled方法规则。 一个方法应该有 void return 类型,一个方法不应该接受任何参数。
- 确保 class 应该使用 @Service 或 @Component Annotation 注释,以便 SpringBoot 可以创建该 class 的对象。
- 调度程序作业的包应该在主应用程序 class 的包下。例如 com.company 是您的主要应用程序 class 包,那么调度程序 class 包应该是 com.company.scheduler,
- 如果您使用的是 Cron 表达式,请确认相同,例如 @Scheduled( cron = "0 0/2 * * * ?"),此 Cron 表达式将每 2 分钟安排一次任务。
欢迎大家在评论中多加点数,对解决问题有帮助。
正如 Swapnil 已经提到的,在使用 cron 时要确保的所有检查点。你应该做的另一件事:
使用以下参考站点 - http://www.freeformatter.com/cron-expression-generator-quartz.html#
始终验证您的 cron 表达式是否格式正确
请检查您是否在 application.properties
“spring.main.lazy-初始化=真”
从 application.properties 中删除它。
即使您的所有配置都正确,这一行也会启用延迟加载,因此您的@Component 将在应用程序启动期间初始化。
我已经创建了一个 Spring 引导应用程序。我已经配置了包含调度程序方法 startService()
的 class。
下面是我的代码:
服务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()");
}
}
主要 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);
}
}
我已将 class 注释为 @Component
,并将方法注释为 @Scheduled(fixedRate = 30000)
,它将 运行 作为调度程序。但是当 运行 应用程序作为 Spring 启动时,调度程序不会触发。控制台显示以下消息:
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'
谁能帮帮我。
我终于能够解决上述问题,我将我的服务包 class EnverseDemoService 从 package com.mk.service;
更改为 com.mk.envers.service;
.这是因为如果主要配置 class EnverseDemoApplication 存在于包 com.mk.envers
中。引导应用程序中的所有其他 classes 应该在合格包中。 Eg: com.mk.envers.*;
也许你可以通过在配置文件中添加@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);
}
}
一定是你忘记在应用中添加@EnableScheduling 注解了class。
public static void main(String[] args) {
context = SpringApplication.run(YouApplication.class, args);
}
在我的例子中,是 lazy-initialization
的值 true
阻止了我的 @Component
在启动时被 Spring 和 @Scheduled
方法加载从来没有 运行.
确保 Spring 启动 lazy initialization 被禁用。
我能够解决这个问题,我忘记提供@service 级别注释, 我已经为@Scheduler 创建了检查列表,请逐一检查每个点,它会帮助您解决问题。
- 检查 SpringBoot Main 上的@EnableScheduling class。
- Scheduled方法需要注解@Scheduled,遵循@Scheduled方法规则。 一个方法应该有 void return 类型,一个方法不应该接受任何参数。
- 确保 class 应该使用 @Service 或 @Component Annotation 注释,以便 SpringBoot 可以创建该 class 的对象。
- 调度程序作业的包应该在主应用程序 class 的包下。例如 com.company 是您的主要应用程序 class 包,那么调度程序 class 包应该是 com.company.scheduler,
- 如果您使用的是 Cron 表达式,请确认相同,例如 @Scheduled( cron = "0 0/2 * * * ?"),此 Cron 表达式将每 2 分钟安排一次任务。
欢迎大家在评论中多加点数,对解决问题有帮助。
正如 Swapnil 已经提到的,在使用 cron 时要确保的所有检查点。你应该做的另一件事: 使用以下参考站点 - http://www.freeformatter.com/cron-expression-generator-quartz.html#
始终验证您的 cron 表达式是否格式正确请检查您是否在 application.properties “spring.main.lazy-初始化=真”
从 application.properties 中删除它。
即使您的所有配置都正确,这一行也会启用延迟加载,因此您的@Component 将在应用程序启动期间初始化。