Reputation: 86
Here is a brief overview of a problem:
I want to call bean lifecycle methods without restarting an application. I need to call interceptor methods (PostConstruct and PreDestroy) of superclasses during reloading of beans in Quarkus and CDI/Weld.
For example, I have end-point for that: /reload - it fires bean reload where I need to go through all beans annotated with my custom annotation and call(if such an annotation presents) posconstr and predestr annotations, BUT I want to preserve bean call order (if bean extends superclass that has such lifecycle callback, then I want to call it first).
What I did to fix it: I used a reflection to call them. But I think it is a kind of dirty fix and hope there should be a more wise solution. There is a solution using CDI:
beanManager.getInjectionTargetFactory(
beanManager.createAnnotatedType(instance.getClass())).createInjectionTarget(
bean).postConstruct(instance)
CDI has getInjectionTargetFactory in beanManager, Quarkus does not support this method. It works but I search solution using Quarkus methods.
Does Quarkus have a possibility to call postConstruct and preDestroy methods of all beans without restarting an application?
Upvotes: 1
Views: 9894
Reputation: 5562
You can use the regular CDI annotations @PostConstruct
and @PreDestroy
on your beans. Quarkus will honor them.
For example:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class MyBean {
@PostConstruct
void init() {
// do something
}
@PreDestroy
void destroy() {
// do something
}
}
Be careful that they will be called at bean initialization and destruction time not application initialization and destruction time, for this there is specific event that you can listen on : https://quarkus.io/guides/lifecycle#listening-for-startup-and-shutdown-events
Upvotes: 5