diff --git a/src/main/java/de/ph87/homeautomation/ServerController.java b/src/main/java/de/ph87/homeautomation/ServerController.java new file mode 100644 index 0000000..ac71098 --- /dev/null +++ b/src/main/java/de/ph87/homeautomation/ServerController.java @@ -0,0 +1,20 @@ +package de.ph87.homeautomation; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("server") +@RequiredArgsConstructor +public class ServerController { + + private final ServerService serverService; + + @GetMapping("shutdown") + public void shutdown() { + serverService.shutdownAsync(); + } + +} diff --git a/src/main/java/de/ph87/homeautomation/ServerService.java b/src/main/java/de/ph87/homeautomation/ServerService.java new file mode 100644 index 0000000..53458f5 --- /dev/null +++ b/src/main/java/de/ph87/homeautomation/ServerService.java @@ -0,0 +1,29 @@ +package de.ph87.homeautomation; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +@EnableAsync +@RequiredArgsConstructor +public class ServerService { + + private final ApplicationContext context; + + @Async + public void shutdownAsync() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // ignore + } + ((ConfigurableApplicationContext) context).close(); + } + +}