简体   繁体   中英

Running SpringBootApplication PostConstruct and PreDestroy

I have troubles with running spring application in docker container (both spring and docker have latest versions in my environment). I want to have healthy life cycle for application class AnalysisServiceBootstrap: to run initialization code with method start() right AFTER creation of AnalysisServiceBootstrap and also to run method stop() right BEFORE destruction of AnalysisServiceBootstrap (I want to run stop() code when someone stops the application).

I have following code:

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

For some reason I can not get method stop() running on docker stop. I tried several ways offered on stackoverflow and other resources, but all of them did not work for me.

I will be glad to have code that works for you (not just some vogue suggestions).

Here is almost exact docker file of mine:

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} 

But you are invited to post here any working docker file that you have.

Thanks a lot.

From the documentation:

docker stop

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

By executing docker stop you are just killing the java (Spring) process.

So what are the guarantees that Spring context will shutdown properly?

The right way to handle SIGTERM in Spring applications is to add shutdown hook.

The final code should look like this:

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

The process is described in the following questions:

Register a shutdown hook to the application as mentioned here Spring Boot shutdown hook

Each SpringApplication will register a shutdown hook with the JVM to ensure that the ApplicationContext is closed gracefully on exit. All the standard Spring lifecycle callbacks (such as the DisposableBean interface, or the @PreDestroy annotation) can be used.

@PreDestroy or implementing the destroy() of DisposableBean gets executed only when we do ctrl+c or docker stop <>. whereas it won't execute if we do docker kill <> or stopping it directly from intelij IDE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM