Please help me understand that how to execute part of the code on Quarkus when starting the application? If possible, discard the link or give an example code. Thanks.
Please help me understand that how to execute part of the code on Quarkus when starting the application? If possible, discard the link or give an example code. Thanks.
Not sure to understand your question but if you want to execute code at startup, the easiest way is to observe (using @Observes) the StartupEvent with a CDI bean:
@ApplicationScoped
public class AppLifecycleBean {
private static final Logger LOGGER = Logger.getLogger("ListenerBean");
void onStart(@Observes StartupEvent ev) {
LOGGER.info("The application is starting...");
}
void onStop(@Observes ShutdownEvent ev) {
LOGGER.info("The application is stopping...");
}
}
You can find more details in our documentation if you need a better understanding of the Quarkus application lifecycle: https://quarkus.io/guides/lifecycle#listening-for-startup-and-shutdown-events
There is an @Startup annotation for Quarkus that will instantiate a service upon startup. It simply automates the listening of the StartupEvent:
For each bean annotated with @Startup a synthetic observer of StartupEvent is generated. The default priority is used.
Works well.