为啥spring boot总是在两个小时内关闭
Posted
技术标签:
【中文标题】为啥spring boot总是在两个小时内关闭【英文标题】:why spring boot always shutdown in two hours为什么spring boot总是在两个小时内关闭 【发布时间】:2016-07-03 22:01:25 【问题描述】:我使用mvn clean package
构建它并获得一个 xxx.jar 文件。
运行$java -jar xxx.jar >> output.log &
启动它,它运行良好。
但大约两个小时后,它关闭了,为什么? 这是一个答案,但对我不起作用>>https://***.com/a/22409655/1767024
这是我的输出日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.1.9.RELEASE)
2016-03-17 17:41:54.784 INFO 20016 --- [ main] com.hello.bibi.App : Starting App on iZ947723zxnZ with PID 20016 (/root/projects/taomi-back/bibi-0.0.1-SNAPSHOT.jar started by root in /root/projects/taomi-back)
2016-03-17 17:41:54.850 INFO 20016 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5cf0ac6e: startup date [Thu Mar 17 17:41:54 CST 2016]; root of context hierarchy
2016-03-17 17:41:56.477 INFO 20016 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false;factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-03-17 17:41:57.557 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'databaseConfig4Bibi' of type [class com.hello.bibi.config.DatabaseConfig4Bibi$$EnhancerBySpringCGLIB$$5c20f114] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:58.434 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'dataSource4bibi' of type [class com.jolbox.bonecp.BoneCPDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.194 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'sqlSessionFactory4bibi' of type [class org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.278 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spiderJdCommentIncrMapper' of type [class org.mybatis.spring.mapper.MapperFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.287 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spiderJdCommentMapper' of type [class org.mybatis.spring.mapper.MapperFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.294 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'spiderJdItemMapper' of type [class org.mybatis.spring.mapper.MapperFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.300 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'userMapper' of type [class org.mybatis.spring.mapper.MapperFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.402 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$364a7a87] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.436 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionAttributeSource' of type [class org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.452 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'transactionInterceptor' of type [class org.springframework.transaction.interceptor.TransactionInterceptor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.461 INFO 20016 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.config.internalTransactionAdvisor' of type [class org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-03-17 17:41:59.992 INFO 20016 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8090
2016-03-17 17:42:00.261 INFO 20016 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-03-17 17:42:00.263 INFO 20016 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.56
2016-03-17 17:42:00.420 INFO 20016 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-03-17 17:42:00.420 INFO 20016 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5574 ms
2016-03-17 17:42:01.119 INFO 20016 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-03-17 17:42:01.124 INFO 20016 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-03-17 17:42:01.519 INFO 20016 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2016-03-17 17:42:01.534 INFO 20016 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2016-03-17 17:42:01.648 INFO 20016 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core 4.3.7.Final
2016-03-17 17:42:01.651 INFO 20016 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2016-03-17 17:42:01.653 INFO 20016 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2016-03-17 17:42:01.991 INFO 20016 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 4.0.5.Final
2016-03-17 17:42:02.076 INFO 20016 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.mysql5Dialect
2016-03-17 17:42:02.155 INFO 20016 --- [ main] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2016-03-17 17:42:02.534 INFO 20016 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-03-17 17:42:02.986 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/bibi/commentIncrTop],methods=[],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]" onto public java.lang.String com.hello.bibi.controller.BibiController.commentIncrTop(javax.servlet.http.HttpServletRequest,int,int)
2016-03-17 17:42:02.986 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/bibi/priceReduce],methods=[],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]" onto public java.lang.String com.hello.bibi.controller.BibiController.priceReduce(javax.servlet.http.HttpServletRequest,int,int)
2016-03-17 17:42:02.987 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/bibi/categoryTree],methods=[],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]" onto public java.lang.String com.hello.bibi.controller.BibiController.categoryTree(javax.servlet.http.HttpServletRequest)
2016-03-17 17:42:02.992 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/taomi/collect],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json;charset=UTF-8],custom=[]" onto public void com.hello.bibi.controller.CollectController.itemInfo(java.lang.String,java.lang.String,java.lang.String)
2016-03-17 17:42:02.994 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//testpage],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.helloWorld()
2016-03-17 17:42:02.994 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//test],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.helloWorld2(java.lang.String,int)
2016-03-17 17:42:02.994 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//test2/id],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.test2(long)
2016-03-17 17:42:02.994 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//test3/str],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.test3(java.lang.String)
2016-03-17 17:42:02.994 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//test4],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.test4(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-03-17 17:42:02.995 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//test5/id/vv/str],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.test5(java.lang.String,java.lang.String)
2016-03-17 17:42:02.995 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[//user],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.HelloWorldController.user()
2016-03-17 17:42:02.995 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/user/test],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public java.lang.String com.hello.bibi.controller.UserController.isNameExist(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-03-17 17:42:02.998 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2016-03-17 17:42:02.998 INFO 20016 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-03-17 17:42:03.043 INFO 20016 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-03-17 17:42:03.043 INFO 20016 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-03-17 17:42:03.526 INFO 20016 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-03-17 17:42:03.528 INFO 20016 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource4bibi' has been autodetected for JMX exposure
2016-03-17 17:42:03.532 INFO 20016 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource4bibi': registering with JMX server as MBean [com.jolbox.bonecp:name=dataSource4bibi,type=BoneCPDataSource]
2016-03-17 17:42:03.729 INFO 20016 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090/http
2016-03-17 17:42:03.731 INFO 20016 --- [ main] com.hello.bibi.App : Started App in 10.01 seconds (JVM running for 10.872)
2016-03-17 17:42:04.217 INFO 20016 --- [nio-8090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-03-17 17:42:04.217 INFO 20016 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-03-17 17:42:04.243 INFO 20016 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 26 ms
2016-03-17 20:01:59.081 INFO 20016 --- [ Thread-2] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5cf0ac6e: startup date [Thu Mar 17 17:41:54 CST 2016]; root of context hierarchy
2016-03-17 20:01:59.085 INFO 20016 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2016-03-17 20:01:59.090 INFO 20016 --- [ Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2016-03-17 20:01:59.092 INFO 20016 --- [ Thread-2] com.jolbox.bonecp.BoneCP : Shutting down connection pool...
2016-03-17 20:01:59.101 INFO 20016 --- [ Thread-2] com.jolbox.bonecp.BoneCP : Connection pool has been shutdown.
App.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class App
public static void main( String[] args )
SpringApplication.run(App.class);
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hello.bibi</groupId>
<artifactId>bibi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>bibi</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
</parent>
<properties>
<main.basedir>$basedir/../..</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mybatis.version>3.2.2</mybatis.version>
<mybatisspring.version>1.2.0</mybatisspring.version>
<app.mainClass>com.hello.bibi.App</app.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>$mybatis.version</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>$mybatisspring.version</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>$app.mainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
您是在控制台中本地启动它,然后在某个可能会终止进程的时候关闭控制台窗口吗? 或者在两小时不活动后出现 SSH 超时? @WillyduPreez 是的,我在云服务器上运行它。其中使用 ssh 登录。关闭控制台后,它仍然作为 Web 应用运行 你是如何开始这个过程的,即使用 nohup ... &?您使用的是哪个云提供商? @WillyduPreez 阿里云,只用命令$java -jar xxx.jar >> output.log &
【参考方案1】:
改为按如下方式运行您的应用程序:
nohup java -jar xxx.jar > output.log 2>&1&
来自Wikipedia:
nohup 是一个忽略 HUP(挂断)信号的 POSIX 命令。按照惯例,HUP 信号是终端警告相关进程注销的方式。如果尚未重定向,通常会发送到终端的输出会发送到名为 nohup.out 的文件。
来自Linux man pages:
nohup - 运行不受挂断影响的命令,输出到非 tty
此外,如果您只是添加 Web 服务器以保持应用程序处于活动状态而不实际使用它,请考虑改用 CountDownLatch:
@Bean
public CountDownLatch closeLatch()
return new CountDownLatch(1);
public static void main(String... args) throws InterruptedException
ApplicationContext ctx = SpringApplication.run(MyApp.class, args);
final CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class);
Runtime.getRuntime().addShutdownHook(new Thread()
@Override
public void run()
closeLatch.countDown();
);
closeLatch.await();
现在要停止您的应用程序,您可以查找进程 ID 并从控制台发出 kill 命令:
kill <PID>
【讨论】:
以上是关于为啥spring boot总是在两个小时内关闭的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring Boot web app 启动后会立即关闭?
为啥spring boot应用程序的main方法已经返回,但应用程序仍然可以接受请求? [关闭]