java - 为什么spring boot总是两小时后关机

标签 java spring spring-boot shutdown application-shutdown

我使用 mvn clean package 构建它并获得一个 xxx.jar 文件。 运行 $java -jar xxx.jar >> output.log & 启动它,它运行良好。

但是大约两个小时后,它就关机了,为什么? 这是一个答案,但对我不起作用 >> https://stackoverflow.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.

应用程序.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>

最佳答案

改为按如下方式运行您的应用程序:

nohup java -jar xxx.jar > output.log 2>&1&

来自 Wikipedia :

nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout. Output that would normally go to the terminal goes to a file called nohup.out if it has not already been redirected.

来自Linux man pages :

nohup - run a command immune to hangups, with output to a non-tty

此外,如果您只是添加 Web 服务器以保持您的应用程序处于 Activity 状态而不实际使用它,请考虑改用 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>

关于java - 为什么spring boot总是两小时后关机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36062525/

相关文章:

java - JOptionPane 与父 JFrame 不对齐

spring - HibernateOptimisticLockingFailureException 将连接标记为 'closed' ?

java - 如何抑制 PMD 中的违规行为?

java - Runnable 执行时间为每个 cron tick 两次

java - 如何使用公共(public)池创建连接池?

java - 在 Selenium Webdriver 中处理表

java - 如何在 Android 中以自定义布局显示 HTML 页面内容

java - 使用 Spring Boot : Could not resolve placeholder 进行 Maven 资源过滤

java - Spring boot JPA中不相关实体的加入查询

java - Springboot @EntityScan 不工作