IncidentMolitServerApplication.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.utic.incident.molit.server;
  2. import com.utic.incident.common.spring.SpringUtils;
  3. import com.utic.incident.molit.server.config.ApplicationConfig;
  4. import com.utic.incident.molit.server.controller.IncidentMolitController;
  5. import com.utic.incident.molit.server.service.ProcessStateService;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.DisposableBean;
  8. import org.springframework.beans.factory.InitializingBean;
  9. import org.springframework.boot.Banner;
  10. import org.springframework.boot.CommandLineRunner;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.boot.builder.SpringApplicationBuilder;
  14. import org.springframework.boot.context.ApplicationPidFileWriter;
  15. import org.springframework.context.ApplicationListener;
  16. import org.springframework.context.annotation.ComponentScan;
  17. import org.springframework.context.event.ContextClosedEvent;
  18. import org.springframework.transaction.annotation.EnableTransactionManagement;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. @Slf4j
  22. @SpringBootApplication
  23. @EnableTransactionManagement
  24. @ComponentScan(basePackages = {"com.utic.incident.common.spring", "com.utic.incident.molit.server.config", "com.utic.incident.molit", "com.utic.incident.common.aspect"})
  25. public class IncidentMolitServerApplication implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, InitializingBean, DisposableBean {
  26. private static boolean isTerminal = false;
  27. public static final String APPLICATION_NAME = "utic-dwdb-server";
  28. public static void main(String[] args) {
  29. SpringApplication application = new SpringApplicationBuilder()
  30. .sources(IncidentMolitServerApplication.class)
  31. .listeners(new ApplicationPidFileWriter("./conf/" + APPLICATION_NAME + ".pid"))
  32. .build();
  33. application.setBannerMode(Banner.Mode.OFF);
  34. application.run(args);
  35. }
  36. @Override
  37. public void run(String... args) throws Exception {
  38. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  39. log.info("");
  40. log.info("");
  41. log.info("************************************************************************************");
  42. log.info("** **");
  43. log.info("** Urban Traffic Information Center **");
  44. log.info("** MOLIT Incident and Event Information Collection System **");
  45. log.info("** **");
  46. log.info("** [ver.1.0] **");
  47. log.info("** startup: {}", sdfDate.format(new Date()));
  48. log.info("************************************************************************************");
  49. ApplicationConfig applicationConfig = SpringUtils.getBean(ApplicationConfig.class);
  50. applicationConfig.setStartSchedule(false);
  51. // 프로세스 상태 저장
  52. ProcessStateService processStateService = SpringUtils.getBean(ProcessStateService.class);
  53. processStateService.processStart();
  54. IncidentMolitController incidentMolitController = SpringUtils.getBean(IncidentMolitController.class);
  55. incidentMolitController.run();
  56. applicationConfig.setStartSchedule(true);
  57. Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  58. log.error("on shutdown hook.");
  59. terminate();
  60. }));
  61. }
  62. public void terminate() {
  63. if (!isTerminal) {
  64. isTerminal = true;
  65. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  66. log.error("************************************************************************************");
  67. log.error("** Application Terminated: {}", sdfDate.format(new Date()));
  68. try {
  69. ApplicationConfig applicationConfig = SpringUtils.getBean(ApplicationConfig.class);
  70. applicationConfig.setStartSchedule(false);
  71. ProcessStateService processStateService = SpringUtils.getBean(ProcessStateService.class);
  72. processStateService.processStop();
  73. } catch (Exception e) {
  74. log.error("** Application Terminated: {}", e.getMessage());
  75. }
  76. log.error("************************************************************************************");
  77. }
  78. }
  79. @Override
  80. public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
  81. log.error("{}", contextClosedEvent);
  82. terminate();
  83. }
  84. @Override
  85. public void destroy() throws Exception {
  86. log.error("Application destroy");
  87. }
  88. @Override
  89. public void afterPropertiesSet() throws Exception {
  90. log.info("Application afterPropertiesSet");
  91. }
  92. }