Homeautomation/src/main/java/de/ph87/homeautomation/ServerService.java

44 lines
1.2 KiB
Java

package de.ph87.homeautomation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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 implements ApplicationContextAware {
private static final int DELAY_SECONDS = 1;
private ConfigurableApplicationContext applicationContext;
@Async
public void shutdown() {
try {
for (int delay = DELAY_SECONDS; delay > 0; delay--) {
log.info("Shutdown in {} second.", delay);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
log.warn("Shutdown interrupted.");
return;
}
applicationContext.close();
System.exit(0);
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.applicationContext = (ConfigurableApplicationContext) ctx;
}
}