简体   繁体   中英

Spring Boot: How do I know if an Application was interrupted in @PreDestroy Method

I there any way to find out if the @PreDestroy Method in a Spring Boot Application was called because CTRL-C was pressed or kill <PID> was called?

I want to differ from the case that the Application regularly stops. (No Daemon, No Web Server)

Background:

I'm using Spring Boot as an Runner that is started in a Docker Container by a schedule. The App does it work and closes itself. The kill <PID> occurs when docker stop <containerid> is called.

You can write your own aspect to log any @PreDestroy execution

@Aspect
@Component
public class CustomAspect {

    @Around("@annotation(javax.annotation.PreDestroy)")
    public Object logPreDestroyExecution(ProceedingJoinPoint joinPoint) throws Throwable {
       // ...
       // get info from joinPoint and log
       // ...
       return joinPoint.proceed();
    } 
}

More detailed example: Spring AOP + AspectJ

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