将对 Spark 的依赖项添加到命令行 Spring 引导应用程序阻止 JVM 退出
Adding a dependency on Spark to a command line Spring Boot application prevents the JVM from exiting
我正在构建一个 REST 客户端。此外,我的路径中还有 Apache Spark。没有 Spark,我可以 运行 作为一个普通的 Spring CommandLineRunner,但是当我添加 Spark 时,我遇到了一个关于竞争记录器实现的问题。
为了解决这个问题,我想我可以更改 Spring 的记录器。这是我的依赖项:
dependencies {
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("org.slf4j:slf4j-api:1.7.1")
compile("org.slf4j:jcl-over-slf4j:1.7.1")
compile("ch.qos.logback:logback-classic:1.0.7")
// compile('com.sparkjava:spark-core:2.2')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
就这样 运行s。当我取消注释这些行时,应用程序挂在最后。这是预期的输出:
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.11/userguide/gradle_daemon.html
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2016-03-07 09:50:36.646 INFO 20150 --- [ main] <packages>.arc.Application : Starting Application on <machine> with PID 20150 (/Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc/build/libs/idiom-pilot-arc-0.1.0.jar started by carbribi in /Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc)
2016-03-07 09:50:36.651 INFO 20150 --- [ main] <packages>.arc.Application : No active profile set, falling back to default profiles: default
2016-03-07 09:50:36.703 INFO 20150 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4ca638be: startup date [Mon Mar 07 09:50:36 EST 2016]; root of context hierarchy
2016-03-07 09:50:37.391 INFO 20150 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-03-07 09:50:37.529 INFO 20150 --- [ main] <packages>.arc.Application : Quote{type='success', value=Value{id=8, quote='I don't worry about my code scaling. Boot allows the developer to peel back the layers and customize when it's appropriate while keeping the conventions that just work.'}}
2016-03-07 09:50:37.532 INFO 20150 --- [ main] <packages>.arc.Application : Started Application in 1.155 seconds (JVM running for 1.507)
2016-03-07 09:50:37.543 INFO 20150 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4ca638be: startup date [Mon Mar 07 09:50:36 EST 2016]; root of context hierarchy
2016-03-07 09:50:37.545 INFO 20150 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
但是当我取消注释这些行时,我得到以下信息:
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2016-03-07 09:55:32.039 INFO 20821 --- [ main] <packages>.arc.Application : Starting Application on <machine> with PID 20821 (/Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc/build/libs/idiom-pilot-arc-0.1.0.jar started by carbribi in /Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc)
2016-03-07 09:55:32.042 INFO 20821 --- [ main] <packages>.arc.Application : No active profile set, falling back to default profiles: default
2016-03-07 09:55:32.090 INFO 20821 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f84341e: startup date [Mon Mar 07 09:55:32 EST 2016]; root of context hierarchy
2016-03-07 09:55:32.674 INFO 20821 --- [ main] org.eclipse.jetty.util.log : Logging initialized @1288ms
2016-03-07 09:55:32.735 INFO 20821 --- [ main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-03-07 09:55:32.737 INFO 20821 --- [ main] org.eclipse.jetty.server.Server : jetty-9.2.15.v20160210
2016-03-07 09:55:32.767 INFO 20821 --- [ main] application : Initializing Spring embedded WebApplicationContext
2016-03-07 09:55:32.767 INFO 20821 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 680 ms
2016-03-07 09:55:32.821 INFO 20821 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-03-07 09:55:32.823 INFO 20821 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@58e48ac1{/,file:/private/var/folders/_r/qfqg_c5n0q9c7kl58145q7njb2xtxx/T/jetty-docbase.7881372241529376843.8080/,AVAILABLE}
2016-03-07 09:55:32.824 INFO 20821 --- [ main] org.eclipse.jetty.server.Server : Started @1439ms
2016-03-07 09:55:33.026 INFO 20821 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-03-07 09:55:33.061 INFO 20821 --- [ main] o.eclipse.jetty.server.ServerConnector : Started ServerConnector@7d141e3b{HTTP/1.1}{0.0.0.0:8080}
2016-03-07 09:55:33.063 INFO 20821 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2016-03-07 09:55:33.626 INFO 20821 --- [ main] <packages>.arc.Application : Quote{type='success', value=Value{id=5, quote='Spring Boot solves this problem. It gets rid of XML and wires up common components for me, so I don't have to spend hours scratching my head just to figure out how it's all pieced together.'}}
2016-03-07 09:55:33.626 INFO 20821 --- [ main] <packages>.arc.Application : Started Application in 1.879 seconds (JVM running for 2.242)
然后执行停止并挂在那里。我可以看到它正在启动 Jetty 服务器。这是它挂起的原因吗?如果是这样,我该如何预防?不管是什么原因,我如何让它结束执行?
I can see its starting a Jetty server. Is this the reason its hanging?
是的。从技术上讲,JVM 没有挂起,但它没有关闭,因为有非守护线程 运行。这些线程由 Jetty 启动,用于处理 HTTP 请求。
If so, how do I prevent it?
Spring Boot 已经启动了一个嵌入式 Jetty servlet 容器,因为它在类路径上找到了 Jetty,因此假定您正在构建一个 Web 应用程序。你需要告诉它事实并非如此。您可以在应用程序的主要方法中将 web 设置为 false:
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(SampleApplication.class).web(false).run(args);
}
}
我正在构建一个 REST 客户端。此外,我的路径中还有 Apache Spark。没有 Spark,我可以 运行 作为一个普通的 Spring CommandLineRunner,但是当我添加 Spark 时,我遇到了一个关于竞争记录器实现的问题。
为了解决这个问题,我想我可以更改 Spring 的记录器。这是我的依赖项:
dependencies {
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("org.slf4j:slf4j-api:1.7.1")
compile("org.slf4j:jcl-over-slf4j:1.7.1")
compile("ch.qos.logback:logback-classic:1.0.7")
// compile('com.sparkjava:spark-core:2.2')
testCompile group: 'junit', name: 'junit', version: '4.11'
}
就这样 运行s。当我取消注释这些行时,应用程序挂在最后。这是预期的输出:
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.11/userguide/gradle_daemon.html
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2016-03-07 09:50:36.646 INFO 20150 --- [ main] <packages>.arc.Application : Starting Application on <machine> with PID 20150 (/Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc/build/libs/idiom-pilot-arc-0.1.0.jar started by carbribi in /Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc)
2016-03-07 09:50:36.651 INFO 20150 --- [ main] <packages>.arc.Application : No active profile set, falling back to default profiles: default
2016-03-07 09:50:36.703 INFO 20150 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4ca638be: startup date [Mon Mar 07 09:50:36 EST 2016]; root of context hierarchy
2016-03-07 09:50:37.391 INFO 20150 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-03-07 09:50:37.529 INFO 20150 --- [ main] <packages>.arc.Application : Quote{type='success', value=Value{id=8, quote='I don't worry about my code scaling. Boot allows the developer to peel back the layers and customize when it's appropriate while keeping the conventions that just work.'}}
2016-03-07 09:50:37.532 INFO 20150 --- [ main] <packages>.arc.Application : Started Application in 1.155 seconds (JVM running for 1.507)
2016-03-07 09:50:37.543 INFO 20150 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4ca638be: startup date [Mon Mar 07 09:50:36 EST 2016]; root of context hierarchy
2016-03-07 09:50:37.545 INFO 20150 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
但是当我取消注释这些行时,我得到以下信息:
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.3.RELEASE)
2016-03-07 09:55:32.039 INFO 20821 --- [ main] <packages>.arc.Application : Starting Application on <machine> with PID 20821 (/Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc/build/libs/idiom-pilot-arc-0.1.0.jar started by carbribi in /Users/carbribi/workspace/code/architecture-pilot/idiom-pilot-arc)
2016-03-07 09:55:32.042 INFO 20821 --- [ main] <packages>.arc.Application : No active profile set, falling back to default profiles: default
2016-03-07 09:55:32.090 INFO 20821 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f84341e: startup date [Mon Mar 07 09:55:32 EST 2016]; root of context hierarchy
2016-03-07 09:55:32.674 INFO 20821 --- [ main] org.eclipse.jetty.util.log : Logging initialized @1288ms
2016-03-07 09:55:32.735 INFO 20821 --- [ main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-03-07 09:55:32.737 INFO 20821 --- [ main] org.eclipse.jetty.server.Server : jetty-9.2.15.v20160210
2016-03-07 09:55:32.767 INFO 20821 --- [ main] application : Initializing Spring embedded WebApplicationContext
2016-03-07 09:55:32.767 INFO 20821 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 680 ms
2016-03-07 09:55:32.821 INFO 20821 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-03-07 09:55:32.823 INFO 20821 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@58e48ac1{/,file:/private/var/folders/_r/qfqg_c5n0q9c7kl58145q7njb2xtxx/T/jetty-docbase.7881372241529376843.8080/,AVAILABLE}
2016-03-07 09:55:32.824 INFO 20821 --- [ main] org.eclipse.jetty.server.Server : Started @1439ms
2016-03-07 09:55:33.026 INFO 20821 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-03-07 09:55:33.061 INFO 20821 --- [ main] o.eclipse.jetty.server.ServerConnector : Started ServerConnector@7d141e3b{HTTP/1.1}{0.0.0.0:8080}
2016-03-07 09:55:33.063 INFO 20821 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)
2016-03-07 09:55:33.626 INFO 20821 --- [ main] <packages>.arc.Application : Quote{type='success', value=Value{id=5, quote='Spring Boot solves this problem. It gets rid of XML and wires up common components for me, so I don't have to spend hours scratching my head just to figure out how it's all pieced together.'}}
2016-03-07 09:55:33.626 INFO 20821 --- [ main] <packages>.arc.Application : Started Application in 1.879 seconds (JVM running for 2.242)
然后执行停止并挂在那里。我可以看到它正在启动 Jetty 服务器。这是它挂起的原因吗?如果是这样,我该如何预防?不管是什么原因,我如何让它结束执行?
I can see its starting a Jetty server. Is this the reason its hanging?
是的。从技术上讲,JVM 没有挂起,但它没有关闭,因为有非守护线程 运行。这些线程由 Jetty 启动,用于处理 HTTP 请求。
If so, how do I prevent it?
Spring Boot 已经启动了一个嵌入式 Jetty servlet 容器,因为它在类路径上找到了 Jetty,因此假定您正在构建一个 Web 应用程序。你需要告诉它事实并非如此。您可以在应用程序的主要方法中将 web 设置为 false:
@SpringBootApplication
public class SampleApplication {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(SampleApplication.class).web(false).run(args);
}
}