简体   繁体   中英

Call a method at Spring Boot application crash

For my use case, I wanted to invoke a method doSomething() under three scenarios:

  1. When my Spring Boot application has started. [Done]
  2. When my Spring Boot application is shutting down. [Done]
  3. When my Spring Boot application has crashed. [Pending]

Basically, an application crash is a type of application shutdown. But I am looking for a way to differentiate between the two. I want to pass the exception which has caused the application to crash to doSomething(ex) as well.

Can I achieve this through ContextClosedEvent or is there another way to achieve the functionality?

is it ok using @PreDestroy?

import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;

@Component
public class MyService  {

    @PreDestroy
    public void predestory() {
        System.out.println("--------using predestoy-----");
    }
}

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