HunterHuang0X7C7
2023-08-12 1c7c28f03215f03e97387d7e6b45ae752c396dcb
去掉无用引用,优化退出卡死和窗口泄露,优化开启后启动报错
1个文件已添加
8个文件已修改
222 ■■■■■ 已修改文件
pom.xml 70 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
readme.md 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
release/SyncTools-1.0.1.RELEASE.war 补丁 | 查看 | 原始文档 | blame | 历史
src/main/deploy/package/mac/SyncTools.icns 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/github/hunter0x7c7/sync/ctrls/Controller.java 85 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/github/hunter0x7c7/sync/model/global/Parameters.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/github/hunter0x7c7/sync/utils/DateUtils.java 14 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/github/hunter0x7c7/sync/utils/PathUtil.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/github/hunter0x7c7/sync/utils/TrayUtil.java 28 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
                            <!-- 绑定自定义JRE路径 -->
                            <!--<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>
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
## 解决系统托盘乱码问题
release/SyncTools-1.0.1.RELEASE.war
Binary files differ
src/main/deploy/package/mac/SyncTools.icns
Binary files differ
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 写文件
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;
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!");
            }
        }
    }
}
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文件。
     * 为了保证调试时获取项目路径,而不是bin路径,增加逻辑: 如果以bin目录接,则返回上一层目录
@@ -20,18 +24,6 @@
     * @return 例如:F:\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;
    }
    /***
     * 这个方法打包位jar文件就无法获取项目路径了。
     * @return
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();
            }
        });
    }
}