use of io.micronaut.context.event.ShutdownEvent in project micronaut-core by micronaut-projects.
the class DefaultBeanContext method stop.
/**
* The close method will shut down the context calling {@link jakarta.annotation.PreDestroy} hooks on loaded
* singletons.
*/
@Override
public synchronized BeanContext stop() {
if (terminating.compareAndSet(false, true)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Stopping BeanContext");
}
publishEvent(new ShutdownEvent(this));
attributes.clear();
// need to sort registered singletons so that beans with that require other beans appear first
List<BeanRegistration> objects = topologicalSort(singletonObjects.values());
Set<Integer> processed = new HashSet<>();
for (BeanRegistration beanRegistration : objects) {
BeanDefinition def = beanRegistration.beanDefinition;
Object bean = beanRegistration.bean;
int sysId = System.identityHashCode(bean);
if (processed.contains(sysId)) {
continue;
}
if (LOG_LIFECYCLE.isDebugEnabled()) {
LOG_LIFECYCLE.debug("Destroying bean [{}] with identifier [{}]", bean, beanRegistration.identifier);
}
processed.add(sysId);
try {
disposeBean(beanRegistration, def.asArgument(), bean, def);
} catch (BeanDestructionException e) {
if (LOG.isErrorEnabled()) {
LOG.error(e.getMessage(), e);
}
}
}
terminating.set(false);
running.set(false);
}
return this;
}
Aggregations