VdsCommServerApplication.java 9.4 KB

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