Sunday, September 5, 2021

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication

StartUp Logging

·         To add additional logging during startup, override logStartupInfo(boolean) in subclass of SpringApplication

Lazy Initialization

·         Beans are initialized when they are needed instead of application start up

·         Pros:

o   Reduces startup - time & memory

·         Cons:

o   Can delay the discovery of a problem in bean misconfiguration until bean is created which otherwise would have been detected at the time of startup

o   Need to make sure that JVM has sufficient memory to accommodate Lazy beans, not only the eager beans, hence fine tuning needs to be done early

·         Enable

o   lazyInitialization method on SpringApplicationBuilder

o   setLazyInitialization method on SpringApplication

o   setting spring.main.lazy-initialization property to true in properties/yaml file

o   Enabling on certain beans - @Lazy(true), Disabling for certain beans @Lazy(false)

Application Availability

·         Application components can retrieve availability state by injecting ApplicationAvailability interface & calling methods on it

·         Spring Boot provides Kubernetes HTTP probes for “Liveness” and “Readiness” with Actuator Health Endpoints

·         Event Order is as follows

o   ApplicationStartingEvent: started but before any processing

o   ApplicationEnvironmentPreparedEvent: Before context is created

o   ApplicationContextInitializedEvent: ApplicationContext is created

o   ApplicationPreparedEvent: After bean definitions are loaded

o   ApplicationStartedEvent: Before application & command-line runners are called

o   AvaiabilityChangeEvent: After LivenessState.CORRECT

o   ApplicationReadyEvent: Aefore application & command-line runners are called

o   AvaiabilityChangeEvent: ReadinessState.ACCEPTING_TRAFFIC

o   ApplicationFailedEvent: If there is exception on startup

·         Apart from these WebServerInitializedEvent and ContextRefreshedEvent are also sent

·         To identify whether the event is for ApplicationContext or child context, listener bean can be implemented ApplicationContextAware so that it can compare if the event published is for it’s context

·         Liveness State

o   Whether its internal state allows it to work correctly or recover by itself if it is failing

o   If Liveness is broken, infrastructure should restart the application

o   Internal state of Spring Boot applications is represented by ApplicationContext

·         Readiness State

o   Whether application is ready to handle the traffic

o   An application is considered ready as soon as ApplicationRunner and CommandLinRunner are called

Application Arguments

·         If you need to access the SpringApplication.run(…) arguments you can inject Spring ApplicationArguments bean

Application and Command-Line Runner

·         If you need to run some specific code before SpringApplication.run(…) completes, you can implement ApplicationRunner or CommandLineRunner interface

·         Both the interface offers same functionality with single run() method which is called just before SpringApplication.run(…) completes

·         Well suited for tasks that should execute after application startup but before it starts accepting traffic

·         If there are multiple application or command-line interface implementation, you can add @Order to execute them in the order intended

·         CommandLineRunner has access to application arguments as String array whereas ApplicationRunner uses ApplicationArguments interface to access arguments passed to SpringApplication.run(…) method

Listener & Runner

·         ApplicationRunner: Access application arguments using ApplicationArguments interface

·         CommandLineRunner: Access application arguments as String array

·         ApplicationListener: Listener for different ApplicationEvent events

·         @EventListener: Listener for different ApplicationEvent events

Application Exit

·         Each Spring Application registers a shutdown hook with JVM to ensure ApplicationContext closes gracefully

Application Startup Tracking

·         During application startup SpringApplication and ApplicationContext perform many tasks related application lifecycle, bean lifecycle or processing application events

·         ApplicationStartup interface implementation allows you to track the application startup sequence using StartupSteps object

·         SpringBoot ships with BufferingApplicationStartup, which is meant for buffering statup steps and draining them into external metrics system

·         Application can ask for bean of type BufferingApplicationStartup in any component

·         SpringBoot can also be configured to setup startup endpoints to provide this information in JSON format

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...