Spring shutdown event that fires just before an ApplicationContext is destroyed?

I am looking for an interceptor or trigger to know that the entire beans context is destroyed, and the applicationcontext instance is about to destroy itself. Therefore, I can do one process at the end of the life of the application.

There is a type of ContextClosedEvent event that is close to what I want to do, but it throws an event after the beans are destroyed. I do this using the close () method of the contexttext application. So it does not fit my need

Any ideas?

Hi

Ali

+4
source share
2 answers

You can use the registerShutDownHook () method of the abstract application's context class. For more information, see this.

UPDATE

Then you should try the @PreDestroy annotation on top of the method in which you want to run something at the end when the spring context is about to destroy.

Hope this helps you. Greetings.

+3
source

Create a bean that implements SmartLifecycle, with getPhase returning Integer.MAX_VALUE. Its stop () method will be executed before any other stop or destroy methods. You can do a cleanup on all resources in live beans.

  @Component public class Terminator implements SmartLifecycle { private boolean started = true; @Override public void stop() { // CLEANUP CODE } @Override public void stop(Runnable callback) { stop(); callback.run(); } @Override public int getPhase() { return Integer.MAX_VALUE; } @Override public boolean isAutoStartup() { return true; } @Override public boolean isRunning() { return started; } }  0   08 . '19  12:17     163     ApplicationContext  WebApplicationContext  Spring MVC? 3    Spring bean       3  Spring       ? 3  ApplicationContext  Spring bean (   ) 1 Spring ThreadPoolTaskExecutor shutdown   Async 1      1    singleton bean  ApplicationContext 0  Spring   ShutdownHook? 0  Spring     0 is System.exit()     ?             Opt out policy content licensed under cc by-sa 4.0 with attribution required 
@Component public class Terminator implements SmartLifecycle { private boolean started = true; @Override public void stop() { // CLEANUP CODE } @Override public void stop(Runnable callback) { stop(); callback.run(); } @Override public int getPhase() { return Integer.MAX_VALUE; } @Override public boolean isAutoStartup() { return true; } @Override public boolean isRunning() { return started; } } 0 08 . '19 12:17 163 ApplicationContext WebApplicationContext Spring MVC? 3 Spring bean 3 Spring ? 3 ApplicationContext Spring bean ( ) 1 Spring ThreadPoolTaskExecutor shutdown Async 1 1 singleton bean ApplicationContext 0 Spring ShutdownHook? 0 Spring 0 is System.exit() ? Opt out policy content licensed under cc by-sa 4.0 with attribution required
@Component public class Terminator implements SmartLifecycle { private boolean started = true; @Override public void stop() { // CLEANUP CODE } @Override public void stop(Runnable callback) { stop(); callback.run(); } @Override public int getPhase() { return Integer.MAX_VALUE; } @Override public boolean isAutoStartup() { return true; } @Override public boolean isRunning() { return started; } } 0 08 . '19 12:17 163 ApplicationContext WebApplicationContext Spring MVC? 3 Spring bean 3 Spring ? 3 ApplicationContext Spring bean ( ) 1 Spring ThreadPoolTaskExecutor shutdown Async 1 1 singleton bean ApplicationContext 0 Spring ShutdownHook? 0 Spring 0 is System.exit() ? Opt out policy content licensed under cc by-sa 4.0 with attribution required
@Component public class Terminator implements SmartLifecycle { private boolean started = true; @Override public void stop() { // CLEANUP CODE } @Override public void stop(Runnable callback) { stop(); callback.run(); } @Override public int getPhase() { return Integer.MAX_VALUE; } @Override public boolean isAutoStartup() { return true; } @Override public boolean isRunning() { return started; } } 0 08 . '19 12:17 163 ApplicationContext WebApplicationContext Spring MVC? 3 Spring bean 3 Spring ? 3 ApplicationContext Spring bean ( ) 1 Spring ThreadPoolTaskExecutor shutdown Async 1 1 singleton bean ApplicationContext 0 Spring ShutdownHook? 0 Spring 0 is System.exit() ? Opt out policy content licensed under cc by-sa 4.0 with attribution required
@Component public class Terminator implements SmartLifecycle { private boolean started = true; @Override public void stop() { // CLEANUP CODE } @Override public void stop(Runnable callback) { stop(); callback.run(); } @Override public int getPhase() { return Integer.MAX_VALUE; } @Override public boolean isAutoStartup() { return true; } @Override public boolean isRunning() { return started; } } 0 08 . '19 12:17 163 ApplicationContext WebApplicationContext Spring MVC? 3 Spring bean 3 Spring ? 3 ApplicationContext Spring bean ( ) 1 Spring ThreadPoolTaskExecutor shutdown Async 1 1 singleton bean ApplicationContext 0 Spring ShutdownHook? 0 Spring 0 is System.exit() ? Opt out policy content licensed under cc by-sa 4.0 with attribution required

Source: https://habr.com/ru/post/1416213/


All Articles