Search in sources :

Example 1 with BeanDestructionException

use of io.micronaut.context.exceptions.BeanDestructionException 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;
}
Also used : BeanDestructionException(io.micronaut.context.exceptions.BeanDestructionException) ShutdownEvent(io.micronaut.context.event.ShutdownEvent) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) ConstructorInjectionPoint(io.micronaut.inject.ConstructorInjectionPoint) InjectionPoint(io.micronaut.inject.InjectionPoint) MethodInjectionPoint(io.micronaut.inject.MethodInjectionPoint) HashSet(java.util.HashSet)

Example 2 with BeanDestructionException

use of io.micronaut.context.exceptions.BeanDestructionException in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method disposeBean.

private <T> void disposeBean(BeanRegistration<T> registration, Argument<T> beanType, T finalBean, BeanDefinition<T> definition) {
    final List<BeanPreDestroyEventListener> preDestroyEventListeners = resolveListeners(BeanPreDestroyEventListener.class, beanType);
    T beanToDestroy = finalBean;
    if (CollectionUtils.isNotEmpty(preDestroyEventListeners)) {
        for (BeanPreDestroyEventListener<T> listener : preDestroyEventListeners) {
            try {
                final BeanPreDestroyEvent<T> event = new BeanPreDestroyEvent<>(this, definition, beanToDestroy);
                beanToDestroy = Objects.requireNonNull(listener.onPreDestroy(event), "PreDestroy event listener illegally returned null: " + listener.getClass());
            } catch (Exception e) {
                throw new BeanDestructionException(definition, e);
            }
        }
    }
    try {
        registration.close();
    } catch (Exception e) {
        throw new BeanDestructionException(definition, e);
    }
    final List<BeanDestroyedEventListener> postDestroyListeners = resolveListeners(BeanDestroyedEventListener.class, beanType);
    if (CollectionUtils.isNotEmpty(postDestroyListeners)) {
        for (BeanDestroyedEventListener<T> listener : postDestroyListeners) {
            try {
                final BeanDestroyedEvent<T> event = new BeanDestroyedEvent<>(this, definition, beanToDestroy);
                listener.onDestroyed(event);
            } catch (Exception e) {
                throw new BeanDestructionException(definition, e);
            }
        }
    }
}
Also used : BeanDestructionException(io.micronaut.context.exceptions.BeanDestructionException) DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) NonUniqueBeanException(io.micronaut.context.exceptions.NonUniqueBeanException) BeanCreationException(io.micronaut.context.exceptions.BeanCreationException) DisabledBeanException(io.micronaut.context.exceptions.DisabledBeanException) BeanDestructionException(io.micronaut.context.exceptions.BeanDestructionException) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) BeanContextException(io.micronaut.context.exceptions.BeanContextException) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) BeanDestroyedEvent(io.micronaut.context.event.BeanDestroyedEvent) BeanPreDestroyEventListener(io.micronaut.context.event.BeanPreDestroyEventListener) BeanPreDestroyEvent(io.micronaut.context.event.BeanPreDestroyEvent) BeanDestroyedEventListener(io.micronaut.context.event.BeanDestroyedEventListener)

Aggregations

BeanDestructionException (io.micronaut.context.exceptions.BeanDestructionException)2 BeanDestroyedEvent (io.micronaut.context.event.BeanDestroyedEvent)1 BeanDestroyedEventListener (io.micronaut.context.event.BeanDestroyedEventListener)1 BeanPreDestroyEvent (io.micronaut.context.event.BeanPreDestroyEvent)1 BeanPreDestroyEventListener (io.micronaut.context.event.BeanPreDestroyEventListener)1 ShutdownEvent (io.micronaut.context.event.ShutdownEvent)1 BeanContextException (io.micronaut.context.exceptions.BeanContextException)1 BeanCreationException (io.micronaut.context.exceptions.BeanCreationException)1 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)1 DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)1 NonUniqueBeanException (io.micronaut.context.exceptions.NonUniqueBeanException)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 ConstructorInjectionPoint (io.micronaut.inject.ConstructorInjectionPoint)1 DisposableBeanDefinition (io.micronaut.inject.DisposableBeanDefinition)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 InitializingBeanDefinition (io.micronaut.inject.InitializingBeanDefinition)1 InjectionPoint (io.micronaut.inject.InjectionPoint)1 MethodInjectionPoint (io.micronaut.inject.MethodInjectionPoint)1