ServerController

This commit is contained in:
Patrick Haßel 2022-10-24 14:14:53 +02:00
parent 16f5a62c9c
commit 82e8aa1712
2 changed files with 49 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}