Eager Initialization

Quite often, there are scenarios where it is good to have something ready to use without having to wait for intial setup. EJB Pooling ensures that Stateless and Message Driven beans can take advantage of eager initialzation. Singleton EJBs (introduced in EJB 3.1) were enriched with @Startup and @DependsOn annotations which has ensured that they don't lag behind their pooled counterparts. Some of the common use cases include bootstrapping global configurations, initializing expensive resources etc.

These annotations are meant for usage with Singleton beans only

@Startup

This annotation marks a Singleton bean for initialization during application startup i.e. the container automatically creates an instance of the bean when the application initializes. This offers the advantage of being able to invoke business logic in an eager and automated fashion (without any manual intervention). One can conveniently leverage the @PostConstruct call back method to execute the bootstrapping logic and also use @PreDestroy to define destruction of expensive resources such as database connections

package ejbap.eagerinit;

import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
public class AppConfigInitBean {

    private String configuration;

    @PostConstruct
    public void init() {
        configuration = UUID.randomUUID().toString();
        System.out.println("Configuration bootstrapped -- " + configuration);
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Destroying configuration -- " + configuration);
        configuration = null;
    }

    public String getConfig() {
        return configuration;
    }
}

It's important to bear in mind that, in the absence of this annotation, a Singleton EJB is instantiated by the container when it is first used/invoked by the client

@DependsOn

Just like @Startup, the @DependsOn annotation is tied to Singleton EJBs. As the name suggests, this annotation is used to establish dependencies between different Singleton beans.

package ejbap.eagerinit;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.DependsOn;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

@Singleton
@Startup
@DependsOn("AppConfigInitBean")
public class DependentSingleton {

    @Inject
    private AppConfigInitBean appConfig;

    @PostConstruct
    public void init() {
        System.out.println("Singleton is ready for use");
        System.out.println("Configuration: "+appConfig.getConfig());
    }

    @PreDestroy
    public void destroy() {
        //Presence of 'AppConfigInitBean' instance is confirmed
        System.out.println("Time to disappear!");
    }
}

One can specify multiple dependent Singleton beans as a part of this annotation. Declaring a dependency on one or multiple Singleton bean(s) ensures that the container

  • Initializes the dependent beans prior to conclusion of the setup process of the singleton (before invocation of its @PostConstruct method)

  • Guarantees the presence of dependent bean instances before the destruction of the singleton bean instance (before completion of its @PreDestroy method)

Next up ...

Let's dive into some of the scheduler and timer related features which EJBs offer...

results matching ""

    No results matching ""