1
我已經通過在war文件中使用jetty嵌入式服務器來設置我的springboot項目。所以基本上,我的戰爭文件是一個可執行文件。如何正常關閉嵌入式碼頭服務器應用程序?
實現ServletContextInitializer我設置主類:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class CrawlerApplication implements ServletContextInitializer {
public static void main(final String[] args) {
SpringApplication.run(CrawlerApplication.class, args);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("mainComponentClass", "com.datalyst.crawler.component.CrawlerServiceTopComponent");
}
}
然後我也有配置的Java文件
@Configuration
public class CrawlerConfig {
@Bean
public EmbeddedServletContainerFactory embeddedServletContainerFactory(){
return new JettyEmbeddedServletContainerFactory(8080);
}
}
這是的build.gradle
apply plugin: 'war'
war {
baseName = 'crawler-service'
version = '0.0.1-SNAPSHOT'
}
configurations {
providedRuntime
}
bootRepackage{
enabled = true
}
dependencies {
compile spec.external.springBootStarterWeb
compile 'org.springframework:spring-web:4.1.6.RELEASE'
providedRuntime("org.springframework.boot:spring-boot-starter-jetty")
}
現在,我設法構建它並通過執行以下命令啓動服務器: nohup java -jar crawler-service-0.0.1-SNAPSHOT.war > crawler-service.log &
。
我使用nohup作爲後臺服務來運行它。
現在,當我想停止該程序時,我必須手動調查ps aux | grep java
的對應PID,並通過執行sudo kill PID
優雅地關閉。但我希望它會更好。
有什麼辦法可以正常關閉服務嗎?例如,在啓動時將STOP_PORT分配給該服務,然後使用該STOP_PORT停止它?
謝謝,但我的服務沒有任何交互UI。此外,「服務器」變量的類型是什麼? server.stop是否調用ServletContextListener.onContextDestroyed()事件? –
服務器的類型爲:org.eclipse.jetty.server.Server。是的,該事件被調用,但我不知道它的server.stop()或handler.stop()。 – Stefan