Spring应用程序无法重启

时间:2019-09-12 12:25:08

标签: java spring-boot java-threads

我正在添加一个端点以重新启动引导应用程序。

我遵循了以下网址的第二意见(通过创建新上下文重新开始):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();
}

我遇到以下错误:

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 个答案:

答案 0 :(得分:2)

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

具有上述devtools依赖性,您可以尝试以下类似方法。

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

}