VdsCommServerApplication.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.its.vds;
  2. import com.its.app.AppUtils;
  3. import com.its.app.utils.OS;
  4. import com.its.app.utils.SysUtils;
  5. import com.its.vds.config.ProcessConfig;
  6. import com.its.vds.process.DbmsDataProcess;
  7. import com.its.vds.service.UnitSystService;
  8. import com.its.vds.service.VdsCtlrService;
  9. import com.its.vds.ui.JTextAreaOutputStream;
  10. import com.its.vds.ui.MainUI;
  11. import com.its.vds.xnettcp.center.CenterTcpServerService;
  12. import com.its.vds.xnettcp.vds.VdsTcpClientCommService;
  13. import com.its.vds.xnettcp.vds.process.VdsDataProcess;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.DisposableBean;
  16. import org.springframework.beans.factory.InitializingBean;
  17. import org.springframework.boot.Banner;
  18. import org.springframework.boot.CommandLineRunner;
  19. import org.springframework.boot.SpringApplication;
  20. import org.springframework.boot.autoconfigure.SpringBootApplication;
  21. import org.springframework.boot.builder.SpringApplicationBuilder;
  22. import org.springframework.boot.context.ApplicationPidFileWriter;
  23. import org.springframework.context.ApplicationContext;
  24. import org.springframework.context.ApplicationListener;
  25. import org.springframework.context.annotation.ComponentScan;
  26. import org.springframework.context.annotation.Configuration;
  27. import org.springframework.context.event.ContextClosedEvent;
  28. import org.springframework.core.io.ClassPathResource;
  29. import org.springframework.scheduling.annotation.EnableAsync;
  30. import javax.swing.*;
  31. import java.awt.*;
  32. import java.awt.event.WindowAdapter;
  33. import java.awt.event.WindowEvent;
  34. import java.io.File;
  35. import java.io.IOException;
  36. import java.io.PrintStream;
  37. import java.net.URL;
  38. @Slf4j
  39. @EnableAsync
  40. @Configuration
  41. @SpringBootApplication
  42. @ComponentScan(basePackages = {"com.its.vds.config", "com.its.vds.dao.mapper", "com.its"})
  43. public class VdsCommServerApplication implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, InitializingBean, DisposableBean {
  44. //public class VdsCommServerApplication implements ApplicationListener<ContextClosedEvent>, InitializingBean, DisposableBean {
  45. private static String applicationName = "vds-comm-server";
  46. public static void main(String[] args) {
  47. File file1 = new File("./conf/" + applicationName + ".pid");
  48. if (file1.exists()) {
  49. System.out.println(System.getProperty("Program Already Running....."));
  50. log.error("Program Already Running.....");
  51. }
  52. if (OS.isWindows()) {
  53. ApplicationContext context = new SpringApplicationBuilder(VdsCommServerApplication.class)
  54. //.web(WebApplicationType.NONE)
  55. .listeners(new ApplicationPidFileWriter("./conf/" + applicationName + ".pid"))
  56. .headless(false)
  57. .bannerMode(Banner.Mode.OFF)
  58. .run(args);
  59. } else {
  60. SpringApplication application = new SpringApplicationBuilder()
  61. .sources(VdsCommServerApplication.class)
  62. .listeners(new ApplicationPidFileWriter("./conf/" + applicationName + ".pid"))
  63. .build();
  64. application.setBannerMode(Banner.Mode.OFF);
  65. application.run(args);
  66. }
  67. }
  68. @Override
  69. public void run(String... args) throws Exception {
  70. ProcessConfig processConfig = (ProcessConfig) AppUtils.getBean(ProcessConfig.class);
  71. log.info("");
  72. log.info("");
  73. log.info("************************************************************************************");
  74. log.info("** **");
  75. log.info("** Intelligent Traffic System **");
  76. log.info("** VDS Communication Server Program. **");
  77. log.info("** **");
  78. log.info("** [ver.1.0] **");
  79. log.info("** {}", processConfig.getId());
  80. log.info("** startup: {}", processConfig.getBootingDateTime());
  81. log.info("************************************************************************************");
  82. // init application
  83. DbmsDataProcess dbmsDataProcess = (DbmsDataProcess)AppUtils.getBean(DbmsDataProcess.class);
  84. dbmsDataProcess.run();
  85. VdsDataProcess tcpServerDataProcess = (VdsDataProcess)AppUtils.getBean(VdsDataProcess.class);
  86. tcpServerDataProcess.run();
  87. UnitSystService unitSystService = (UnitSystService)AppUtils.getBean(UnitSystService.class);
  88. unitSystService.loadMaster();
  89. unitSystService.updateUnitSystStts(true);
  90. VdsCtlrService ctlrService = (VdsCtlrService)AppUtils.getBean(VdsCtlrService.class);
  91. ctlrService.loadDb();
  92. ctlrService.updateCtlrStts(true);
  93. VdsTcpClientCommService vdsCommClientService = (VdsTcpClientCommService)AppUtils.getBean(VdsTcpClientCommService.class);
  94. vdsCommClientService.run();
  95. CenterTcpServerService centerService = (CenterTcpServerService)AppUtils.getBean(CenterTcpServerService.class);
  96. centerService.run();
  97. // schedule enable
  98. processConfig.setStartSchedule(true);
  99. if (OS.isWindows()) {
  100. SwingUtilities.invokeLater(() -> {
  101. String sysTime = SysUtils.getSysTimeStr();
  102. //JFrame.setDefaultLookAndFeelDecorated(true);
  103. JFrame frame = new JFrame("VDS 통신 서버 - [" + sysTime + "]");
  104. MainUI UI = new MainUI(frame);
  105. try {
  106. ClassPathResource file = new ClassPathResource("static/image/application.png");
  107. URL imgURL = file.getURL();
  108. frame.setIconImage(new ImageIcon(imgURL).getImage());
  109. } catch (IOException e) {
  110. log.error("Not found application icon image");
  111. }
  112. frame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  113. frame.setContentPane(UI.getRootPanel());
  114. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  115. frame.addWindowListener(new WindowAdapter() {
  116. @Override
  117. public void windowClosing(WindowEvent e) {
  118. if (JOptionPane.showConfirmDialog(UI.getRootPanel(), "시스템을 종료 하시겠습니까?", "시스템 종료", 0) == 0) {
  119. System.exit(0);
  120. } else {
  121. frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  122. }
  123. }
  124. });
  125. frame.pack();
  126. frame.setBounds(100, 100, 1000, 600);
  127. frame.setLocationRelativeTo(null);
  128. frame.setVisible(true);
  129. JTextArea logArea = UI.getTaLog();
  130. logArea.setText(null);
  131. JTextAreaOutputStream out = new JTextAreaOutputStream(logArea);
  132. System.setOut(new PrintStream(out));
  133. UI.LoadControllerInfo();
  134. });
  135. }
  136. }
  137. public void terminateApplication() {
  138. UnitSystService unitSystService = (UnitSystService) AppUtils.getBean(UnitSystService.class);
  139. unitSystService.updateUnitSystStts(false);
  140. VdsCtlrService ctlrService = (VdsCtlrService) AppUtils.getBean(VdsCtlrService.class);
  141. ctlrService.updateCtlrStts(false);
  142. CenterTcpServerService centerService = (CenterTcpServerService)AppUtils.getBean(CenterTcpServerService.class);
  143. centerService.stop();
  144. VdsTcpClientCommService vdsCommClientService = (VdsTcpClientCommService)AppUtils.getBean(VdsTcpClientCommService.class);
  145. vdsCommClientService.shutdown();
  146. }
  147. @Override
  148. public void onApplicationEvent(ContextClosedEvent event) {
  149. log.error("Application Terminated: {}", event.getTimestamp());
  150. terminateApplication();
  151. }
  152. @Override
  153. public void afterPropertiesSet() throws Exception {
  154. //System.err.println("InitializingBean 인터페이스 구현 메서드입니다. 'Bean'이 생성될 때 마다 호출되는 메서드 입니다.");
  155. }
  156. @Override
  157. public void destroy() throws Exception {
  158. //System.err.println("DisposableBean 인터페이스 구현 메서드입니다. 'Bean'이 소멸될 때 마다 호출되는 메서드입니다");
  159. }
  160. /*@Bean
  161. public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
  162. return args -> {
  163. System.out.println("Let's inspect the beans provided by Spring Boot:");
  164. String[] beanNames = ctx.getBeanDefinitionNames();
  165. Arrays.sort(beanNames);
  166. for (String beanName : beanNames) {
  167. System.out.println(beanName);
  168. }
  169. };
  170. }*/
  171. }