8

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

There is this event type ContextClosedEvent, which is close to the thing that I wanna do but, it throws the event after destruction of beans. I thing it comes with the close() method of the applicationcontext. So it doesn't fit to my need

Any ideas?

Regards

Ali

nobeh
  • 9,784
  • 10
  • 49
  • 66
Neron
  • 1,500
  • 7
  • 30
  • 52
  • Out of curiosity, `ClosedContextEvent` [means](http://static.springsource.org/spring/docs/current/spring-framework-reference/html/beans.html#context-functionality-events) that at this moment there are no alive beans in the context, so, what are you interested in achieving? Isn't this actually what you need? – nobeh Jun 05 '12 at 20:54
  • it is firing itself at the beginning of the disposal process. Like, for ex, at the beginning of context.close(). I want it at the end of it – Neron Jun 13 '12 at 06:41
  • Please read once more the link I provided. It exactly means the way you need. – nobeh Jun 13 '12 at 06:59

2 Answers2

4

You can use registerShutDownHook() method of the abstract application context class. For more details have a look at this.

UPDATE

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

Hope this helps you. Cheers.

Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44
  • Is there any decent sample for this usage? because, the samples that I have seen are not providing to link a method. Is there any basic usage sample like this? – Neron Jun 06 '12 at 05:12
  • Please check the below link for example. http://www.roseindia.net/tutorial/spring/spring3/ioc/registershutdownhook.html – Japan Trivedi Jun 06 '12 at 05:28
  • 1
    I am looking for an entry point to run something in the end when the spring context is about be the destroyed. This sample is not something like that – Neron Jun 06 '12 at 05:57
  • 1
    They are all bean based solutions dude, I need to override context disposal mechanism of spring I think. I solved it depend-on attribute but , it is not a good solutions of course. I am still lookin for a new one – Neron Jun 12 '12 at 08:12
0

Create a bean implementing SmartLifecycle, with a getPhase returning Integer.MAX_VALUE. Its stop() method will be executed before any other stop or destroy methods. You can there do cleanup on every resources in living 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;
        }

    }
turiot
  • 1
  • 1