Saturday, April 18, 2020

Restarting Spring Boot Application !

Facebook

Restarting Spring Boot Application  !

You have several options to restart Spring boot Application . You can restart either of the following three options. Based on your requirement you can choose one of the following.

  1. Using spring-boot-devtools
  2.            Add your Gradle dependency
        dependencies{ 
                    compileOnly("org.springframework.boot:spring-boot-devtools")
      }
        After that Add, you can simply add an end poing on your controller class and have the following:
              org.springframework.boot.devtools.restart.Restarter.getInstance().restart();   

    The drawback of using tools is you cannot make a good used of @Cacheable . it might cause ClassCastException while reading from the cache.

  3. Using RestartEndPoint from 
  4.    Add your Gradle dependency
      dependencies{ 
         compile "org.springframework.boot:spring-boot-starter-actuator"
      }
        After that Add, you can simply add an end on your controller class and have the following:
         @Autowired
         RestartEndpoint restartEndpoint;
         
        And call the following methond from your controller :
        restartEndpoint.restart() 
         
    You might see NullPointer Exception while trying to bring the application up specially when you have UnderTow as your servlet container 
     3.   And the third one is reading the current context and restarting it. You can simply add the following method on the Main Class and invoke from your controller
       public static void restart() {
          ApplicationArguments args = appContext.getBean(ApplicationArguments.class);
          Thread thread = new Thread(() -> {
          appContext.close();
          SpringApplication springApplication = new SpringApplication(MyBootApplication.class);
          springApplication.addListeners(new ApplicationPidFileWriter());
          appContext =springApplication.run(args.getSourceArgs());
        });
       thread.setDaemon(false);
       thread.start();
      }

0 comments:

Post a Comment

I love to entertain onymous user Comment !

Pages

 ©mytechtoday.com 2006-2010

 ©Mytechtoday

TOP