Skip to main content

ApplicationStartedEvent and ApplicationReadyEvent code example

Both ApplicationStartedEvent and ApplicationReadyEvent executes once service is started but there is little difference that ApplicationReadyEvent executes when application is ready to serve the request. Will see the implementation for both in below code.
Since both are starting after application is up, we can use annotation based listener in this case. We will see the implementation In two different ways.

Using Annotation

To use annotation based listener we can define below code in any spring managed bean, like Component, Configuration etc.
 @EventListener(ApplicationReadyEvent.class)
 public void applicationReadyEvent() {
  System.out.println("Executing ApplicationReadyEvent...");
 }
 @EventListener(ApplicationStartedEvent.class)
 public void applicationStartedEvent() {
  System.out.println("Executing ApplicationStartedEvent...");
 }

Registering with SpringApplication

Below code shows the event listener creation with SpringApplication.
@SpringBootApplication
public class SpringBootTutorialApplication implements AsyncConfigurer{

 public static void main(String[] args) {
  SpringApplication app = new SpringApplication(SpringBootTutorialApplication.class);
  
  app.addListeners((ApplicationReadyEvent event)->{
   System.out.println("Executing ApplicationReadyEvent...");
  });

  app.addListeners((ApplicationStartedEvent event)->{
   System.out.println("Executing ApplicationStartedEvent...");
  });
  
  //start the application
  app.run(args);
 }

Output

Event execution statements are highlighted in below output

2019-10-26 16:22:24.136  INFO 14298 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-26 16:22:24.139  INFO 14298 --- [           main] com.demo.SpringBootTutorialApplication   : Started SpringBootTutorialApplication in 6.076 seconds (JVM running for 6.594)
Executing ApplicationStartedEvent...
Executing ApplicationReadyEvent...

Comments

  1. This is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanks Best Event App Services Provider

    ReplyDelete

Post a Comment

Popular Posts

Asynchronous REST service implementation in Spring boot

In this tutorial we will see how to create an asynchronous REST service endpoint using Spring boot application. Asynchronous service works in a way that it will not block the client request and do the processing in separate thread. When work is complete the response returned to the client so our service will be able to handle more client requests at the same time, compare to synchronous processing model. Let's understand how it is working in synchronous mode. In such server/client application at server side it has a pool of threads which are serving the request. If a request received by a thread then it will be blocked until it send the response back to client. In this case if processing doesn't take much time it will be able to process it quickly and accept other client requests but there could be one situation when all threads are busy and not able to accept the new client requests. To overcome of such problems, asynchronous processing model introduced for REST service

Setting up kerberos in Mac OS X

Kerberos in MAC OS X Kerberos authentication allows the computers in same domain network to authenticate certain services with prompting the user for credentials. MAC OS X comes with Heimdal Kerberos which is an alternate implementation of the kerberos and uses LDAP as identity management database. Here we are going to learn how to setup a kerberos on MAC OS X which we will configure latter in our application. Installing Kerberos In MAC we can use Homebrew for installing any software package. Homebrew makes it very easy to install the kerberos by just executing a simple command as given below. brew install krb5 Once installation is complete, we need to set the below export commands in user's profile which will make the kerberos utility commands and compiler available to execute from anywhere. Open user's bash profile: vi ~/.bash_profile Add below lines: export PATH=/usr/local/opt/krb5/bin:$PATH export PATH=/usr/local/opt/krb5/sbin:$PATH export LDFLAGS=&

SpringBoot - @ConditionalOnProperty example for conditional bean initialization

@ConditionalOnProperty annotation is used to check if specified property available in the environment or it matches some specific value so it can control the execution of some part of code like bean creation. It may be useful in many cases for example enable/disable service if specific property is available. Below are the attributes which can be used for property check. havingValue - Provide the value which need to check against specified property otherwise it will check that value should not be false. matchIfMissing - If true it will match the condition and execute the annotated code when property itself is not available in environment. name - Name of the property to be tested. If you want to test single property then you can directly put the property name as string like "property.name" and if you have multiple properties to test then you can put the names like {"prop.name1","prop.name2"} prefix - It can be use when you want to apply some prefix to

jaxb2-maven-plugin to generate java code from XSD schema

In this tutorial I will show how to generate the Java source code from XSD schema. I will use jaxb2-maven-plugin to generate the code using XSD file which will be declared in pom.xml to make it part of build, so when maven build is executed it will generate the java code using XSD. Class generation can be controlled in plugin configuration. Maven changes (pom.xml) Include below plugin in your pom.xml. Here we have done some configuration under configuration section as given below. schemaDirectory : This is the directory where I keep my schema (XSD file). outputDirectory : This is the java source location where I want to generate the Java files. If it is not given then by default it will be generate inside target folder. clearOutputDir : If this property is true then it will generate the classes on each build otherwise it will generate only if output directory is empty. <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</art