• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Destroy Singleton Spring beans and terminate Spring container

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot terminate the JVM (e.g. by calling System.exit(1) for stand-alone) as such, any code written in registerShutdownHook will not get invoked.

On the contrary, if an exception condition is raised during the start-up of an application server, I want to dispose any Singleton bean and terminate the Spring container.

I tried using the following separately:

configurableListableBeanFactory.destroySingletons();
abstractApplicationContext.destroy();
abstractApplicationContext.close();

None of them seemed to work.

The abstractApplicationContext (aplicationContext interface for that matter) was still NOT null. The number of beans alive was still the same, as was before calling destroySingletons(), destroy(), close() API.

My goal is to dispose the bean instances themselves.

No clean-up is necessary in any bean (e.g. by writing destroy-method in XML) as these beans do not hold reference to anything like datasource etc.

But I just want to remove all the initialized singleton Spring beans. The Spring beans are not lazy, they are all Singleton, do not have destroy-method in XML, neither do implement DisposeBean interface.

I was thinking that one of the following API would remove the beans from scope and will also make application context null. API referring to are:

configurableListableBeanFactory.destroySingletons();
abstractApplicationContext.destroy();
abstractApplicationContext.close();

But it did not work.

What am I missing?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not duplicate post. This is a duplicate of a newer thread you created, and since the other has more information, I am going to close this one.

usually, just calling close() on your ApplicationContext is enough to remove the beans. But if you internally have references to any of those beans, then it wouldn't be garbage collected. And on that note, you are not guaranteed on when garbage collection does happen, so maybe your code is checking too quickly.

Mark
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic