Helidon: Startup and Shutdown hook

When you build a real-time microservice application, you may need a listener for startup and shutdown events. This might be very simple and straight forward when we implement our application using Servlet APIs. We can simply provide an implementation of ServletContextListener and listen to a specific lifecycle event of our choice. Now the question is how this can be achieved using the Helidon framework? The answer is also very simple but we may need to take a different approach using the CDI lifecycle.

How?

@ApplicationScoped
public class DailyApiBootListener {
    public void init(@Observes @Initialized(ApplicationScoped.class)
                             final Object init) {
        System.out.println("Init part is called");
        System.out.println(init.getClass().getName());
    }

    public void beforeDestroy(@Observes @BeforeDestroyed(ApplicationScoped.class)
                                Object init) {
        System.out.println("Before Destroy is called");
    }

    public void destroy(@Observes @Destroyed(ApplicationScoped.class)
                                Object init) {
        System.out.println("Destroy is called");
    }
}

Inside the methods, we can write the necessary code and achieve whatever is needed in our business use-cases.

ApplicationScoped is also an annotation provided by CDI specification. All the CDI beans will be scanned automatically during the start of the server. So that we can easily attach our listeners during the lifecycle of the CDI beans.

I hope this would solve your use-cases. Please share this article if this is useful to you.

{ Happy coding }

API Evangelist | Chief Hustler @workrituals.com | Habit Coach | Madras Java User Group Leader
78FollowersFollow
0SubscribersSubscribe

Latest articles

Flutter: How does column layout work?

Layouts are the fundamental starting points for developing Flutter applications. There are many layout options are available by default. I am going...

Flutter: Stateless vs Stateful widgets

Flutter is a mobile application development framework developed by Google. You need to understand the fundamental building blocks before you dig deeper...

Why Flutter is a great technology for makers &...

As per the wikipedia, Flutter is an open source UI development kit created by Google. It is used to...

Why 12-Factory methodology matters to your Microservice development?

The 12-Factor application development methodology and why is it important for your microservices strategy

When to use Firebase Cloud functions?

Recently I got a question from one of the Indie makers that, when to use Firebase Cloud Functions? This...

Similar articles