1

I am adding an endpoint to restart my boot application.

I have followed 2nd opinion (Restart by Creating a New Context) of the following url : https://www.baeldung.com/java-restart-spring-boot-app

@RestController
 public class RestartController {
 @RequestMapping(value = "/restart-admin", method = RequestMethod.POST)
    public void restart() {    
            ActivitiAdminCrmMain.restart();
    }

}
public class ActivitiAdminCrmMain {
private static ConfigurableApplicationContext context;

public static void main(String[] args) {
    context = SpringApplication.run(ActivitiAdminCrmMain.class, args);
}

public static void restart() {
    ApplicationArguments args = context.getBean(ApplicationArguments.class);

    Thread thread = new Thread(() -> {
        context.close();
        context = SpringApplication.run(ActivitiAdminCrmMain.class, args.getSourceArgs());
    });

    thread.setDaemon(false);
    thread.start();
}

I am getting below error :

Exception in thread "Thread-4" java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/spring.factories]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
    at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1325)
    at org.apache.catalina.loader.WebappClassLoaderBase.findResources(WebappClassLoaderBase.java:948)
    at java.lang.ClassLoader.getResources(ClassLoader.java:1142)
    at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:110)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:393)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:261)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at storebrand.activiti.admin.ActivitiAdminCrmMain.lambda$0(ActivitiAdminCrmMain.java:35)
    at java.lang.Thread.run(Thread.java:748)
1
  • Did you try to use org.springframework.boot.devtools.restart.Restarter.getInstance().restart() ? Commented Sep 12, 2019 at 12:35

1 Answer 1

1
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

With above devtools dependency, you can try something like below.

@RestController
 public class RestartController {
 @RequestMapping(value = "/restart-admin", method = RequestMethod.POST)
    public void restart() {    
        org.springframework.boot.devtools.restart.Restarter.getInstance().restart();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.