0

I have a situation where one of my classes needs to perform some initialization tasks when the application starts up. This class depends on another component that also needs to be initialized at startup. I am using Quarkus and have annotated the dependent component with @Startup to ensure it gets initialized early. However, I am observing that the initialization method in my main class, which listens for the StartupEvent, is executing before the dependent component is initialized. This leads to issues such as the dependent component not being available when my main class tries to use it, resulting in exceptions. How can I ensure that the dependent component is fully initialized before my main class starts its initialization process in Quarkus?

In Spring I could use the @DependsOn which does not exist in Quarkus

I tried to use the @Priority but it seems to not work in my case.

Does anyone have an idea how I can control the initialisation sequence for those beans?

1 Answer 1

0

@Startup#value() can be used to specify the priority of the generated observer method and thus affects the ordering. In other words, if you annotate the class with @Startup(10) then it's translated to something like:

void start(@Observes @Priority(10) StartupEvent event, YourBean bean) {
   // some code that makes sure the bean is initialized
}
2
  • One thing to me mindful of - if you are using @Startup, you most likely don't want that for @Dependent beans as such bean will be created for the purpose of the observer and destroyed afterwards ([documented here][1]), which follows specification behavior. [1]: quarkus.io/guides/lifecycle#startup_annotation
    – Siliarus
    Commented Jun 14 at 7:35
  • There's a legitimate use case for @Startup @Dependent beans. I.e. if you need to perform some stateless startup logic. Note that @Startup can be also declared on methods. See also quarkus.io/guides/lifecycle#startup_annotation. Commented Jun 17 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.