0

I am working on a Spring project. I want to use scheduler in it and want to schedule it on a variable date. This date has to be taken from database. Is it possible to fetch data from database before server is getting started?

1 Answers1

0

Two solutions come to my mind:

  • @PostConstruct annotated method of some @Component:

    @Component
    public class MyBean 
    {
    
       @PostConstruct
       public void init() 
       {
          // Blocking DB call could go here
       }
    
    }
    
  • Application Events. For the ApplicationReadyEvent:

    @Component
    public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent>
    {
    
       @Override
       public void onApplicationEvent(ApplicationReadyEvent event) 
       {
          // DB call could go here
          //
          // Notice that if this is a web services application, it
          // would potentially be serving requests while this method
          // is being executed
       }
    
    }
    
gears
  • 690
  • 3
  • 6