Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Registering a shutdown hook in Spring 2.5

Tags:

java

spring

I have a spring application which is not calling bean destroy methods on shutdown. I've seen references to this being due to instantiation in a beanRefFactory, and that this can be circumvented through manually calling registerShutdownHook() on an the application context.This method seems to have disappeared from spring between versions 2.0 - 2.5.

Can someone point me in the direction of how this is now done?

Thanks.

like image 348
Steve B. Avatar asked Dec 01 '09 16:12

Steve B.


1 Answers

This method is still available in ConfigurableApplicationContext and implemented by AbstractApplicationContext.

So you might be able to do this

ApplicationContext ctx = ...;
if (ctx instanceof ConfigurableApplicationContext) {
    ((ConfigurableApplicationContext)ctx).registerShutdownHook();
}

Alternatively, you could simply call ((ConfigurableApplicationContext)ctx).close() yourself while closing down the application or using your own shutdown hook:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run(){
       if (ctx instanceof ConfigurableApplicationContext) {
           ((ConfigurableApplicationContext)ctx).close();
       }
    }
 });
like image 108
sfussenegger Avatar answered Sep 27 '22 23:09

sfussenegger