[英]Spring Boot Starter-Web tries to connect to Mongo at startup
[英]How starter-web prevent spring-boot exit?
使用 spring-boot-starter-web 时,spring-boot 应用程序启动后不会退出。
使用 Jetbrain IDEA,有一个图标显示 spring-boot 启动完成:
但如果我使用:
public static void main ( String[] args ){
SpringApplication.run(Application.class, args);
Thread.sleep(Long.MAX_VALUE);
}
或者
public class MyRunner implements ApplicationRunner{
public void run() {
Thread.sleep(Long.MAX_VALUE);
}
}
可以让 spring-boot 继续运行,但 IDEA 图标会永远加载,所以与 starter-web 相比必须是不同的方式。
Update1:这两种方法将导致 SpringBootTest 永远等待
问题: spring-boot-starter-web 阻止 spring-boot 退出的代码是什么?
我不知道是什么原因,但是没有网络服务器,只有当任何其他线程仍然处于活动状态(非守护线程)时,您才能防止关闭。 另一个线程中的无限循环将阻止程序退出。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println("main started");
new Thread(() -> {
System.out.println("thread started");
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
while (true) {
System.out.println(context);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}
}).start();
System.out.println("main finished");
}
}
输出示例
main started
main finished
thread started
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.7)
2022-06-24 22:34:31.648 INFO 8084 --- [ Thread-1] o.s.boot.SpringApplication : Starting application using Java 1.8.0_312 on DESKTOP with PID 8084 (started by user in C:\Users\user\IdeaProjects\demo)
2022-06-24 22:34:31.650 INFO 8084 --- [ Thread-1] o.s.boot.SpringApplication : No active profile set, falling back to 1 default profile: "default"
2022-06-24 22:34:31.985 INFO 8084 --- [ Thread-1] org.quartz.impl.StdSchedulerFactory : Using default implementation for ThreadExecutor
2022-06-24 22:34:31.991 INFO 8084 --- [ Thread-1] org.quartz.core.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2022-06-24 22:34:31.991 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.3.2 created.
2022-06-24 22:34:31.991 INFO 8084 --- [ Thread-1] org.quartz.simpl.RAMJobStore : RAMJobStore initialized.
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.3.2) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.3.2
2022-06-24 22:34:31.992 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@501627dc
2022-06-24 22:34:32.009 INFO 8084 --- [ Thread-1] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now
2022-06-24 22:34:32.010 INFO 8084 --- [ Thread-1] org.quartz.core.QuartzScheduler : Scheduler quartzScheduler_$_NON_CLUSTERED started.
2022-06-24 22:34:32.015 INFO 8084 --- [ Thread-1] o.s.boot.SpringApplication : Started application in 0.601 seconds (JVM running for 1.051)
org.springframework.context.annotation.AnnotationConfigApplicationContext@74ed4e3, started on Fri Jun 24 22:34:31 MSK 2022
org.springframework.context.annotation.AnnotationConfigApplicationContext@74ed4e3, started on Fri Jun 24 22:34:31 MSK 2022
org.springframework.context.annotation.AnnotationConfigApplicationContext@74ed4e3, started on Fri Jun 24 22:34:31 MSK 2022
and so on...
只有 1 个依赖项(只是随机启动器,不是网络,这里不需要石英)
org.springframework.boot:spring-boot-starter-quartz
只是非守护线程中的无限循环......
回答问题
spring-boot-starter-web 阻止 spring-boot 退出的代码是什么
spring-boot-web 创建使用非守护程序侦听器线程的 Web 服务器 bean(因为您希望它们处于活动状态并运行以等待传入连接并处理它们)。
这会使您的代码到达main
方法的末尾,但是 JVM 不会退出,因为它有非守护线程正在运行。
如果你想让你的应用程序保持活跃,你需要做同样的事情 - 最简单的方法是例如在 @Slongtong 建议的 bean 中创建一个线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.