计划任务应该在每次执行时启动Spring应用程序吗?

时间:2019-01-11 15:51:57

标签: spring spring-boot

我正在运行带有计划任务的Spring Boot应用程序。它分为两个Maven项目,我们称它们为“ lib”和“ app”。

其方法用@Scheduled注释的类位于'lib'项目中。它不包含任何用@SpringBootApplication注释的类,也不包含任何启动spring应用程序的类。 Scheduled bean的外观如下:

@Component
public class Refresher {

    @Scheduled(fixedRate = 30000)
    public void refresh() {
        ...
    }

'lib'项目还具有一个简单的SchedulingConfigurer实现,它告诉Spring将ThreadPoolTaskScheduler用于其计划任务。

另一方面,“ app”项目依赖于“ lib”,并具有一个用@SpringBootApplication注释的类,并像往常一样启动它:

@SpringBootApplication(scanBasePackages = {"x.y", "x.y.z"})
@EnableScheduling
public class ExampleService {

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

两个Maven项目都具有以下内容:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
</parent>

因此,当ExampleService启动时,我发现它找到Refresher并每30秒执行一次refresh方法,但是日志中出现了一些奇怪的现象。当Example服务启动时,它会打印:

2019-01-11 18:18:27.647  INFO 6428 --- [           main] x.y.z.ExampleService  : Started ExampleService in 32.613 seconds (JVM running for 40.37)

这很棒,它告诉我主应用程序已启动。但是,每当执行Refresher时,我每30秒就会得到以下几行:

2019-01-11 18:18:58.596  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414: startup date [Fri Jan 11 18:18:58 MSK 2019]; root of context hierarchy
2019-01-11 18:18:58.637  INFO 6428 --- [nio-8080-exec-3] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$7c355e31] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
... some logging from the refresh() method
2019-01-11 18:19:01.020  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1262db58: startup date [Fri Jan 11 18:19:01 MSK 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] o.s.boot.SpringApplication               : Started application in 3.585 seconds (JVM running for 73.771)
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1262db58: startup date [Fri Jan 11 18:19:01 MSK 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414
2019-01-11 18:19:01.035  INFO 6428 --- [nio-8080-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@66e31414: startup date [Fri Jan 11 18:18:58 MSK 2019]; root of context hierarchy

注意“在3.585秒内启动了应用程序(JVM运行73.771)”。这每30秒发生一次。看起来为了运行Scheduled bean,Spring每次都会运行一个新的单独的Spring应用程序。为什么这样做,可以避免吗? Scheduled bean是否可以作为当前启动的Spring Boot应用程序的一部分运行?

0 个答案:

没有答案