Startup Object

Overview;

The Startup Object pattern simple denotes an object which will execute upon application start. This is typically within the context and after the framework is wired. In our case Spring.

The goal of these objects are to do some last minute configuration of the system to properly prepare it to handle requests. These objects are also helpful to consolidate exactly what is starting up, in what order, how long it is taking, etc.

Groovy Example;


@Log4j // just a groovy logger
@Component // this will be picked up when we run our scanner
class AbstractServerStartup implements ApplicationListener {

  boolean created = false
  
  @Override
  void onApplicationEvent(ApplicationEvent event) {
    if(created)
      return
    if(event instanceof ContextRefreshedEvent) {
      created = true
      log.info "Running post startup scripts."
      long start = System.currentTimeMillis()
      onStartup()
      long elapsedTime = System.currentTimeMillis() - start
      log.info "Completed post startup scripts. " + elapsedTime + "ms" // just for timing our startups, how much weight we add to server
    }
  }

  abstract def onStartup() // extend and override this method or fill in some code here
}



0 comments:

Disqus for Wookets Wove