编程语言
首页 > 编程语言> > java – 运行SpringBootApplication PostConstruct和PreDestroy

java – 运行SpringBootApplication PostConstruct和PreDestroy

作者:互联网

我在docker容器中运行spring应用程序时遇到了麻烦(spring和docker在我的环境中都有最新版本).
我希望为应用程序类AnalysisServiceBootstrap提供健康的生命周期:在创建AnalysisServiceBootstrap之后立即使用方法start()运行初始化代码,并在销毁AnalysisServiceBootstrap之前运行方法stop()(我想在某人时运行stop()代码)停止申请).

我有以下代码:

package com.pack;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class AnalysisServiceBootstrap {

    // called OK on docker "start <containerId>"
    @PostConstruct
    public void start() throws Exception {
        // some init code
    }

    // NOT called on "docker stop <containerId>"
    @PreDestroy
    public void stop() {
       // some destroy code
    }

    public static void main(String[] args) {
        SpringApplication.run(AnalysisServiceBootstrap.class, args);
    }
}

出于某种原因,我无法在docker stop上运行方法stop().
我尝试了stackoverflow和其他资源提供的几种方法,但所有这些方法都不适用于我.

我很乐意拥有适合您的代码(不仅仅是一些时尚建议).

这里几乎是我的docker文件:

FROM *********:6556/service-jvm

ARG SERVICE_JAR_FILE

ENV SERVICE_NAME service
ENV HTTP_PORT 603
ENV HTTPS_PORT 604
ENV SERVICE_JAR /opt/my/project/${SERVICE_JAR_FILE}
EXPOSE ${HTTP_PORT} ${HTTPS_PORT}
COPY ${SERVICE_JAR_FILE} /opt/my/project/${SERVICE_JAR_FILE}

CMD java -Xms1024m -Xmx1024m -dump:"/opt/my/project/dumppath" -javaagent:/opt/my/project/agent.jar -Djav.awt.headless=true -jar ${SERVICE_JAR} 

但是你被邀请在这里发布你有的任何工作的docker文件.

非常感谢.

解决方法:

从文档:

docker stop

Stop one or more running containers
The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL

通过执行docker stop,你只是在杀死java(Spring)进程.

那么Spring上下文将正确关闭的保证是什么?

在Spring应用程序中处理SIGTERM的正确方法是添加shutdown hook.

最终代码应如下所示:

package com.pack;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class AnalysisServiceBootstrap {

    @PostConstruct
    public void start() throws Exception {
        // some init code
    }

    @PreDestroy
    public void tearDown() {
       // some destroy code
    }

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                // write here any instructions that should be executed
                tearDown();
            }   
        });


        SpringApplication.run(AnalysisServiceBootstrap.class, args);
    }
}

该过程在以下问题中描述:

> How to close Spring beans properly after received a SIGTERM?
> How to handle a SIGTERM

标签:java,docker,spring,spring-boot-2,sigkill
来源: https://codeday.me/bug/20190717/1487197.html