From 1c7c28f03215f03e97387d7e6b45ae752c396dcb Mon Sep 17 00:00:00 2001
From: HunterHuang0X7C7 <1125607007@qq.com>
Date: 星期六, 12 八月 2023 16:58:15 +0800
Subject: [PATCH] 去掉无用引用,优化退出卡死和窗口泄露,优化开启后启动报错

---
 src/main/java/com/github/hunter0x7c7/sync/model/global/Parameters.java |    2 
 src/main/java/com/github/hunter0x7c7/sync/utils/DateUtils.java         |   14 ++
 readme.md                                                              |    3 
 src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java          |   20 ++---
 pom.xml                                                                |   70 ++++++++++------
 src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java        |   85 ++++++++++++++------
 src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java          |   28 +++++-
 release/SyncTools-1.0.1.RELEASE.war                                    |    0 
 src/main/deploy/package/mac/SyncTools.icns                             |    0 
 9 files changed, 145 insertions(+), 77 deletions(-)

diff --git a/pom.xml b/pom.xml
index f507727..492ed3b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -15,14 +15,15 @@
 
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-        <maven.compiler.source>1.8</maven.compiler.source>
-        <maven.compiler.target>1.8</maven.compiler.target>
+        <maven.compiler.source>9</maven.compiler.source>
+        <maven.compiler.target>9</maven.compiler.target>
 
         <junit.version>5.3.1</junit.version>
+        <javafx.version>11</javafx.version>
         <lombok.version>1.18.2</lombok.version>
-        <jetty.version>2.3.0</jetty.version>
-        <slf4j.version>1.8.0-beta2</slf4j.version>
-        <log4j.version>1.2.17</log4j.version>
+        <!--<jetty.version>2.3.0</jetty.version>-->
+        <!--<slf4j.version>1.8.0-beta2</slf4j.version>-->
+        <!--<log4j.version>1.2.17</log4j.version>-->
 
         <retrofit.version>2.3.0</retrofit.version>
         <rxjava.version>1.1.8</rxjava.version>
@@ -30,7 +31,7 @@
         <gson.version>2.8.2</gson.version>
         <okhttp.version>3.4.1</okhttp.version>
 
-        <common-data.version>0.2.8</common-data.version>
+        <!--<common-data.version>0.2.8</common-data.version>-->
     </properties>
 
 
@@ -44,14 +45,14 @@
             </snapshots>
         </repository>-->
 
-        <!--<repository>
-            <id>nexus</id>
-            <name>nexus</name>
+        <repository>
+            <id>public_maven</id>
+            <name>public_maven</name>
             <url>http://www.zgleiju.com:8964/nexus/repository/public_maven/</url>
             <snapshots>
                 <enabled>true</enabled>
             </snapshots>
-        </repository>-->
+        </repository>
 
     </repositories>
 
@@ -80,7 +81,7 @@
                             <bundleJre>true</bundleJre>
                             <!-- 缁戝畾鑷畾涔塉RE璺緞 -->
                             <!--<jrePath>D:\Develop\Java\jdk-9.0.1\jre</jrePath>-->
-                            <jrePath>/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre</jrePath>
+                            <jrePath>/Library/Java/JavaVirtualMachines/jre-9.0.1.jre/Contents/Home</jrePath>
                             <!-- 鐢熸垚瀹夎鍖� -->
                             <generateInstaller>false</generateInstaller>
                             <!-- 涓嶄娇鐢ㄧ鐞嗗憳韬唤鎵撳紑绋嬪簭 -->
@@ -88,12 +89,12 @@
                             <!-- 灏嗘寚瀹氭枃浠跺す鍖呭惈鍒扮敓鎴愮殑绋嬪簭璺緞 -->
                             <!-- 鎵嬪姩寮曞叆棰濆璧勬簮-->
                             <additionalResources>
-                            <!--<additionalResource>F:\workspace_java\JavaFx\SyncTools\libs</additionalResource>-->
-                            <additionalResource>/Users/hunter/workspace/JavaFx/SyncTools/lib</additionalResource>
-                        </additionalResources>
-                        <!-- 褰撳墠鎵撳寘鐨勫钩鍙� -->
+                                <!--<additionalResource>F:\workspace_java\JavaFx\SyncTools\libs</additionalResource>-->
+                                <additionalResource>/Users/hunter/workspace/JavaFx/SyncTools/lib</additionalResource>
+                            </additionalResources>
+                            <!-- 褰撳墠鎵撳寘鐨勫钩鍙� -->
                             <platform>mac</platform>
-                            <name>SyncToolsV1.0.1</name>
+                            <name>SyncTools</name>
                             <!-- exe 鍚嶇О锛岄�氳繃寮曠敤鐨勬柟寮� -->
                             <displayName>${project.name}</displayName>
                             <!-- 铏氭嫙鏈哄弬鏁� -->
@@ -112,10 +113,13 @@
                             </winConfig>-->
                             <!--<linuxConfig></linuxConfig>-->
                             <macConfig>
-                                <!-- 杞欢鍥炬爣 -->
+                                <appId>com.github.hunter0x7c7.SyncTools</appId>
                                 <icnsFile>
                                     /Users/hunter/workspace/JavaFx/SyncTools/src/main/deploy/package/mac/SyncTools.icns
                                 </icnsFile>
+                                <generateDmg>false</generateDmg>
+                                <generatePkg>false</generatePkg>
+                                <relocateJar>false</relocateJar>
                             </macConfig>
                         </configuration>
                     </execution>
@@ -130,14 +134,14 @@
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.1</version>
                 <!--<configuration>
-                    <source>8</source>
-                    <target>8</target>
+                <source>8</source>
+                <target>8</target>
                 </configuration>-->
                 <configuration>
                     <verbose>true</verbose>
                     <fork>true</fork>
                     <!--<executable>D:\Develop\Java\jdk-9.0.1\bin\javac</executable>-->
-                     <executable>/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/javac</executable>
+                    <executable>/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/javac</executable>
                 </configuration>
             </plugin>
         </plugins>
@@ -152,6 +156,18 @@
             <scope>test</scope>
         </dependency>
 
+        <!-- JavaFx -->
+        <dependency>
+            <groupId>org.openjfx</groupId>
+            <artifactId>javafx-base</artifactId>
+            <version>${javafx.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.openjfx</groupId>
+            <artifactId>javafx-fxml</artifactId>
+            <version>${javafx.version}</version>
+        </dependency>
+
         <!-- lombok -->
         <dependency>
             <groupId>org.projectlombok</groupId>
@@ -161,7 +177,7 @@
         </dependency>
 
         <!-- Log -->
-        <dependency>
+        <!--<dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>${log4j.version}</version>
@@ -175,7 +191,7 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>${slf4j.version}</version>
-        </dependency>
+        </dependency>-->
 
         <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
         <!-- <dependency>
@@ -238,14 +254,14 @@
             <version>${common-data.version}</version>
         </dependency>-->
         <!-- apache io鎿嶄綔閫氱敤jar鍖� -->
-        <dependency>
+        <!--<dependency>
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>
             <version>2.4</version>
-        </dependency>
+        </dependency>-->
 
         <!-- httpclient 鏀寔jar -->
-        <dependency>
+        <!--<dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.3.5</version>
@@ -254,9 +270,9 @@
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpmime</artifactId>
             <version>4.3.5</version>
-        </dependency>
+        </dependency>-->
 
-
+        <!-- SQL Server -->
         <dependency>
             <groupId>com.microsoft.sqlserver</groupId>
             <artifactId>mssql-jdbc</artifactId>
diff --git a/readme.md b/readme.md
index c9ba258..8ea819a 100644
--- a/readme.md
+++ b/readme.md
@@ -6,11 +6,12 @@
 https://blog.csdn.net/qq_29428909/article/details/122103131
 JavaFx 鑷畾涔夌郴缁熸墭鐩�
 https://www.jianshu.com/p/6058e7a48f0b
-
+~~~~
 .ico鏂囦欢鍦ㄧ嚎杞崲
 http://www.ico51.cn
 
 .icns鏂囦欢鍦ㄧ嚎杞崲 
+https://www.alltoall.net/png_icns
 https://cloudconvert.com/png-to-icns
 
 ## 瑙e喅绯荤粺鎵樼洏涔辩爜闂
diff --git a/release/SyncTools-1.0.1.RELEASE.war b/release/SyncTools-1.0.1.RELEASE.war
new file mode 100644
index 0000000..8efed5f
--- /dev/null
+++ b/release/SyncTools-1.0.1.RELEASE.war
Binary files differ
diff --git a/src/main/deploy/package/mac/SyncTools.icns b/src/main/deploy/package/mac/SyncTools.icns
index d053bfa..f317ab3 100644
--- a/src/main/deploy/package/mac/SyncTools.icns
+++ b/src/main/deploy/package/mac/SyncTools.icns
Binary files differ
diff --git a/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java b/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
index d99e4b2..2e34353 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java
@@ -114,10 +114,10 @@
     }
 
 
-    @Override
+    /*@Override
     protected void finalize() throws Throwable {
         super.finalize();
-    }
+    }*/
 
     private void init() {
         mCompositeDisposable = new CompositeDisposable();
@@ -219,13 +219,14 @@
                                     setInputText(tvInputTargetDbName, cb.getTargetDbName());
                                     setInputText(tvInputFreq, cb.getFreqValue());
                                     cbSelectFreqUnit.getSelectionModel().select(cb.getFreqUnit());
+
+                                    //榛樿鍚姩鍚屾鍔熻兘
+                                    if (cb.isStartSync()) {
+                                        startSync();    //鍚姩鍚屾鍔熻兘
+                                    }
                                 }
                             });
 
-                            //榛樿鍚姩鍚屾鍔熻兘
-                            if (cb.isStartSync()) {
-                                startSync();    //鍚姩鍚屾鍔熻兘
-                            }
                         } else if (BuildType.equals(BuildTypeEnum.DEBUG)) {
                             try {
                                 String srcHost = ConfigProperties.SRC_HOST;
@@ -338,24 +339,54 @@
 
     //鐐瑰嚮锛氶��鍑�
     private void clickExit() {
-        if (isSyncing()) {
-            stopSync();//鍋滄鍚屾
-        }
-        //绯荤粺鎵樼洏
-        SystemTray tray = SystemTray.getSystemTray();
-        if (tray != null) {
-            TrayIcon[] icons = tray.getTrayIcons();
-            for (TrayIcon icon : icons) {
-                if (icon == null) continue;
-                //閫�鍑轰箣鍓嶅厛绉婚櫎绯荤粺鎵樼洏鍥炬爣
-                if (APPID.equals(icon.getActionCommand())) {
-                    tray.remove(icon);
-                }
-            }
-        }
-        //閫�鍑�
-        Platform.exit();
+        addSubscribe(Observable.just(true)
+                .subscribeOn(Schedulers.newThread())
+                .observeOn(Schedulers.io())
+                .doOnNext(new Consumer<Object>() {
+                    @Override
+                    public void accept(Object o) throws Exception {
+                        if (isSyncing()) {
+                            stopSync();//鍋滄鍚屾
+                        }
+                    }
+                })
+                .doOnNext(new Consumer<Object>() {
+                    @Override
+                    public void accept(Object o) throws Exception {
+
+                        //妫�鏌ョ郴缁熸槸鍚︽敮鎸佹墭鐩�
+                        if (SystemTray.isSupported()) {
+                            //绯荤粺鎵樼洏
+                            SystemTray tray = SystemTray.getSystemTray();
+                            if (tray != null) {
+                                TrayIcon[] icons = tray.getTrayIcons();
+                                for (TrayIcon icon : icons) {
+                                    if (icon == null) continue;
+                                    //閫�鍑轰箣鍓嶅厛绉婚櫎绯荤粺鎵樼洏鍥炬爣
+                                    if (APPID.equals(icon.getActionCommand())) {
+                                        tray.remove(icon);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                })
+                .subscribe(new Consumer<Boolean>() {
+                    @Override
+                    public void accept(Boolean aBoolean) throws Exception {
+
+                        TrayUtil.getInstance().exitApp();//閫�鍑�
+                    }
+                }, new Consumer<Throwable>() {
+                    @Override
+                    public void accept(Throwable e) throws Exception {
+                        e.printStackTrace();
+
+                        TrayUtil.getInstance().exitApp();//閫�鍑�
+                    }
+                }));
     }
+
 
     //鐐瑰嚮锛氬叧浜�
     private void clickAbout() {
@@ -394,7 +425,7 @@
                                 , targetFreq, freqSelect, startSync);
 
                         //鍐欏叆鏂囦欢
-                        String path = PathUtil.projectPath + "\\config.json";
+                        String path = PathUtil.getConfigFilePath("config.json");
                         String content = JsonUtil.toJson(cb);
 
                         //浣跨敤 BufferedWriter 鍐欐枃浠�
@@ -461,7 +492,7 @@
     //浠庢湰鍦伴厤缃枃浠朵腑鑾峰彇閰嶇疆淇℃伅
     private ConfigBean getSaveConfig() {
         try {
-            String json = FileUtil.bufferedReader(PathUtil.projectPath + "\\config.json");
+            String json = FileUtil.bufferedReader(PathUtil.getConfigFilePath("config.json"));
             return JsonUtil.fromJson(json, ConfigBean.class);
         } catch (Exception e) {
             e.printStackTrace();
@@ -567,7 +598,7 @@
         operate.setAlignment(Pos.CENTER_RIGHT);
         operate.setSpacing(8);
 //        operate.setStyle("-fx-background-color: #F0F0F0");
-        operate.setPadding(new Insets(12, 20, 6, 20));
+        operate.setPadding(new Insets(12, 20, 12, 20));
         operate.getChildren().addAll(defButton, cancelButton);
 
         VBox vBox = new VBox();
@@ -858,7 +889,7 @@
                         }
                         cb.setStartSync(startSync);
 
-                        String path = PathUtil.projectPath + "\\config.json";
+                        String path = PathUtil.getConfigFilePath("config.json");
                         String content = JsonUtil.toJson(cb);
 
                         //浣跨敤 BufferedWriter 鍐欐枃浠�
diff --git a/src/main/java/com/github/hunter0x7c7/sync/model/global/Parameters.java b/src/main/java/com/github/hunter0x7c7/sync/model/global/Parameters.java
index 5cc8209..317e50d 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/model/global/Parameters.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/model/global/Parameters.java
@@ -6,7 +6,7 @@
 
     public static final int VersionCode = 2;
     public static final String VersionName = "1.0.1";
-    public static final String BuildDate = "230808";
+    public static final String BuildDate = "230812";
     public static final BuildTypeEnum BuildType = BuildTypeEnum.RELEASE;
 
 
diff --git a/src/main/java/com/github/hunter0x7c7/sync/utils/DateUtils.java b/src/main/java/com/github/hunter0x7c7/sync/utils/DateUtils.java
index 664f290..aa4f0fe 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/utils/DateUtils.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/utils/DateUtils.java
@@ -1,14 +1,11 @@
 package com.github.hunter0x7c7.sync.utils;
 
-import org.apache.http.annotation.Immutable;
-import org.apache.http.util.Args;
 
 import java.lang.ref.SoftReference;
 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
-@Immutable
 public final class DateUtils {
     public static final String PATTERN_Def = "yyyy-MM-dd HH:mm:ss";
     public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
@@ -110,4 +107,15 @@
             THREADLOCAL_FORMATS.remove();
         }
     }
+
+    public static class Args {
+
+        public static void notNull(Object o, String msg) {
+            if (o == null) {
+                throw new RuntimeException(msg + " is not null!");
+            }
+        }
+    }
+
+
 }
diff --git a/src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java b/src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java
index e64898f..bf9c364 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java
@@ -13,6 +13,10 @@
      */
     public static final String projectPath = initProjectPathAndDebug();
 
+    public static String getConfigFilePath(String cfgName) {
+        return String.format("%s%s%s", projectPath, getFileSeparator(), cfgName);
+    }
+
     /***
      * 鑾峰彇椤圭洰鏍硅矾寰勶紝鏃犺鏄墦鍖呮垚jar鏂囦欢銆�
      * 涓轰簡淇濊瘉璋冭瘯鏃惰幏鍙栭」鐩矾寰勶紝鑰屼笉鏄痓in璺緞锛屽鍔犻�昏緫锛� 濡傛灉浠in鐩綍鎺ワ紝鍒欒繑鍥炰笂涓�灞傜洰褰�
@@ -20,18 +24,6 @@
      * @return 渚嬪锛欶:\eclipse\work\JavaFX\AddressApp\build\dist
      */
     private static String initProjectPathAndDebug() {
-
-        if (SystemUtil.isWindows()) {
-            System.out.println(".....isWindows....");
-        }
-        if (SystemUtil.isMacOs()) {
-            System.out.println("....isMacOs.....");
-        }
-        if (SystemUtil.isLinux()) {
-            System.out.println("......isLinux...");
-        }
-
-
         URL url = PathUtil.class.getProtectionDomain().getCodeSource().getLocation();
         String filePath = null;
         try {
@@ -59,6 +51,10 @@
         return null;
     }
 
+    public static String getFileSeparator() {
+        return File.separator;
+    }
+
     /***
      * 杩欎釜鏂规硶鎵撳寘浣峧ar鏂囦欢灏辨棤娉曡幏鍙栭」鐩矾寰勪簡銆�
      * @return
diff --git a/src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java b/src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java
index ec9f0cd..50f3170 100644
--- a/src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java
+++ b/src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java
@@ -2,17 +2,15 @@
 
 
 import com.github.hunter0x7c7.sync.model.interfaces.Callback;
+import com.github.hunter0x7c7.sync.model.storage.Session;
 import javafx.application.Platform;
 import javafx.stage.Stage;
-
-import java.util.List;
 
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionListener;
 
 import static com.github.hunter0x7c7.sync.model.global.Parameters.APPID;
-import static com.github.hunter0x7c7.sync.model.global.Parameters.AppNameString;
 
 public class TrayUtil {
 
@@ -64,7 +62,7 @@
 
         TrayIcon trayIcon = new TrayIcon(img, tooltip, popup);
         //璁剧疆鍥炬爣灏哄鑷姩閫傚簲
-        trayIcon.setImageAutoSize(true);
+        //trayIcon.setImageAutoSize(true);
         trayIcon.setActionCommand(APPID);
         trayIcon.addActionListener(new ActionListener() {
             @Override
@@ -102,8 +100,7 @@
             if (tray != null) {
                 tray.remove(trayIcon);
             }
-            //閫�鍑�
-            Platform.exit();
+            exitApp();
         });
         //鐐瑰嚮鍏抽棴鎸夐挳鏃堕殣钘忓満鏅�
         stage.setOnCloseRequest(event -> {
@@ -114,4 +111,23 @@
             });
         });
     }
+
+
+    //閫�鍑�
+    public void exitApp() {
+        //UI绾跨▼
+        Platform.runLater(new Runnable() {
+            @Override
+            public void run() {
+                //閫�鍑哄墠鍏堝叧闂獥鍙�
+                Stage stage = Session.getInstance().getPrimaryStage();
+                if (stage != null) {
+                    stage.close();
+                }
+                //閫�鍑�
+                Platform.exit();
+            }
+        });
+    }
+
 }

--
Gitblit v1.9.1