gpt4 book ai didi

java - 向命令行 Spring Boot 应用程序添加对 Spark 的依赖可防止 JVM 退出

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:55 25 4
gpt4 key购买 nike

我正在构建一个 REST 客户端。此外,我的路径中还有 Apache Spark。如果没有 Spark,我可以作为普通的 Spring CommandLineRunner 运行,但是当我添加 Spark 时,我会遇到有关竞争记录器实现的问题。

为了解决这个问题,我想我可以更改 Spring 的 Logger。这是我的依赖项:

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'
}

当它运行时。当我取消注释这些行时,应用程序将在最后挂起。这是预期的输出:

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);
}

}

关于java - 向命令行 Spring Boot 应用程序添加对 Spark 的依赖可防止 JVM 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com