Erikas Laptevas
Erikas Laptevas

Reputation: 25

Restarting Java Spring Boot Application

first of all i just want to tell that i already read multiple methods and none of them is working for me. All i need is just restart the application whenever i call method.

what i have tried so far:

@Service
    public class RestartService {

        @Autowired
        private RestartEndpoint restartEndpoint;

        public void restartApp() {
            Thread restartThread = new Thread(() -> restartEndpoint.restart());
            restartThread.setDaemon(false);
            restartThread.start();
        }
    }

another one:

@SpringBootApplication
public class RestfulWebservicesApplication {


private static ConfigurableApplicationContext context;

    public static void main(String[] args) throws IOException {
        SpringApplication.run(RestfulWebservicesApplication.class, args);


    }

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

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

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

and this:

public class OneMoreRestart {

    @GetMapping("/restart")
    void restart() {
        Thread restartThread = new Thread(() -> {
            try {
                Thread.sleep(1000);
                Main.restart();
            } catch (InterruptedException ignored) {
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
    }

}

also i tried methods that were posted here: https://www.baeldung.com/java-restart-spring-boot-app

i have tried all the methods that i found in google or here none of them are working for me....

i usually get nullpointer exception, im guys i know im a complete noob but i never tought that restarting the application might be so hard. What am i missing here or what am i doing here wrong????

Upvotes: 1

Views: 1342

Answers (1)

steve
steve

Reputation: 11

you need to modify it as shown below

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

Upvotes: 1

Related Questions