cuixubin 4 年之前
父節點
當前提交
946438da40

+ 54 - 29
src/main/java/com/persagy/filemove/app/SagaFileMoveApp.java

@@ -1,11 +1,12 @@
 package com.persagy.filemove.app;
 
 import com.persagy.filemove.dto.SagaFileMoveDTO;
-import com.persagy.filemove.dto.WorkerValidParam;
 import com.persagy.filemove.service.SagaFileMoveService;
 import com.persagy.filemove.util.StringTools;
 import javafx.application.Application;
+import javafx.application.Platform;
 import javafx.collections.FXCollections;
+import javafx.concurrent.Task;
 import javafx.geometry.Insets;
 import javafx.geometry.Pos;
 import javafx.scene.Node;
@@ -96,7 +97,6 @@ public class SagaFileMoveApp extends Application {
     /** 进度指示器 */
     private ProgressBar progressBar = new ProgressBar(0);
 
-
     public static void main(String[] args) {
         launch(args);
     }
@@ -138,13 +138,20 @@ public class SagaFileMoveApp extends Application {
         progressBar.setPrefSize(appWidth * .65, 25);
 
         btnValid.setOnAction(e -> {
-            progressBar.setProgress(-1);
-            validParam();
-            progressBar.setProgress(1);
+            progressBar.progressProperty().unbind();
+            progressBar.setProgress(0);
+            Task<Boolean> worker = paramValidWorker();
+            progressBar.progressProperty().bind(worker.progressProperty());
+            worker.messageProperty().addListener((obv, ov, nv)->{
+                System.out.println(nv);
+            });
+            new Thread(worker).start();
         });
 
+
         btnExecute.setOnAction(e -> {
-            if(validParam()) {
+            String errorMsg = getParamValidResult();
+            if(errorMsg != null && errorMsg.startsWith("success")) {
                 // 记录当前参数
                 writeInfoToLogFile("执行参数:" + dto.toString());
 
@@ -168,7 +175,30 @@ public class SagaFileMoveApp extends Application {
         });
     }
 
-
+    public Task<Boolean> paramValidWorker() {
+        return new Task<Boolean>() {
+
+            @Override
+            protected Boolean call() throws Exception {
+            updateProgress(-1, 1);
+            String result = getParamValidResult();
+            updateProgress(1, 1);
+
+            Platform.runLater(() -> {
+                if(result.startsWith("success")) {
+                    if(result.contains("warning")) {
+                        showTipsWarn(result);
+                    }else {
+                        showTipsSuccess(result);
+                    }
+                }else {
+                    showTipsError(result);
+                }
+            });
+            return true;
+            }
+        };
+    }
 
 
     /**
@@ -191,44 +221,39 @@ public class SagaFileMoveApp extends Application {
 
     /**
      * 校验参数
-     * @return
+     * @return 参数无误,返回"success-信息";
      */
-    private boolean validParam() {
-        showTipsInfo("");
-        boolean result = true;
-        WorkerValidParam worker = new WorkerValidParam(this);
-        worker.start();
+    private String getParamValidResult() {
+        String errorMsg = null;
 
         if(StringTools.isBlank(dto.dpf.getValue()) || StringTools.isBlank(dto.pjId.getValue()) ||
                 StringTools.isBlank(dto.imgFromUrl.getValue()) || StringTools.isBlank(dto.imgFromSysId.getValue()) ||
                 StringTools.isBlank(dto.imgFromSecret.getValue()) || StringTools.isBlank(dto.imgToUrl.getValue()) ||
                 StringTools.isBlank(dto.imgToSysId.getValue()) || StringTools.isBlank(dto.imgToSecret.getValue())) {
-            showTipsError("必填参数不能为空!");
-            result = false;
+            errorMsg = "必填参数不能为空!";
         }
 
-        if(result && !service.validParamDpfAndPj(this)) {
-            showTipsError("获取不到项目信息!请确保数据平台地址和项目id参数正确。");
-            result = false;
+        if(errorMsg == null && !service.validParamDpfAndPj(this)) {
+            errorMsg = "获取不到项目信息!请确保数据平台地址和项目id参数正确。";
         }
 
-        if(result && !service.validParamImgFromUrl()) {
-            showTipsError("[文件服务-From]访问不通!请确保URL正确。");
-            result = false;
+        if(errorMsg == null && !service.validParamImgFromUrl()) {
+            errorMsg = "[文件服务-From]访问不通!请确保URL正确。";
         }
 
-        if(result && !service.validParamImgToUrl()) {
-            showTipsError("[文件服务-To]访问不通!请确保URL正确。");
-            result = false;
+        if(errorMsg == null && !service.validParamImgToUrl()) {
+            errorMsg = "[文件服务-To]访问不通!请确保URL正确。";
         }
 
-        if(result && dto.imgFromUrl.getValue().equals(dto.imgToUrl.getValue())) {
-            showTipsWarn("参数校验通过。警告:两个文件服务的URL相同!");
-        }else {
-            showTipsSuccess("校验通过");
+        if(errorMsg == null) {
+            if(dto.imgFromUrl.getValue().equals(dto.imgToUrl.getValue())) {
+                errorMsg = "success-校验通过。warning-两个文件服务的URL相同!";
+            }else {
+                errorMsg = "success-校验通过";
+            }
         }
 
-        return result;
+        return errorMsg;
     }
 
     public void showTipsSuccess(String info) {

+ 39 - 0
src/main/java/com/persagy/filemove/dto/ProgressWorker.java

@@ -0,0 +1,39 @@
+package com.persagy.filemove.dto;
+
+import com.persagy.filemove.app.SagaFileMoveApp;
+import javafx.concurrent.Task;
+
+public class ProgressWorker {
+    public static Task<Boolean> paramValidWorker(SagaFileMoveApp app) {
+        return new Task<Boolean>() {
+
+            @Override
+            protected Boolean call() throws Exception {
+                int num = 1;
+                long sleepTime = 200;
+                Double nextSleepTime = 200.0;
+
+                while(!"".equals(app.getTips())) {
+                    updateProgress(num++, 100);
+                    try {
+                        Thread.sleep(sleepTime);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                    nextSleepTime = nextSleepTime * 1.06;
+                    sleepTime = nextSleepTime.longValue();
+
+                    if(num == 101) {
+                        break;
+                    }
+                }
+                for(int i=0; i<5; i++) {
+                    updateMessage("sleep 0.5s -- " + i);
+                    updateProgress(i + 1, 5);
+                    Thread.sleep(100);
+                }
+                return true;
+            }
+        };
+    }
+}

+ 12 - 3
src/main/java/com/persagy/filemove/dto/WorkerValidParam.java

@@ -5,7 +5,7 @@ import javafx.beans.property.SimpleDoubleProperty;
 
 public class WorkerValidParam extends Thread {
     private SagaFileMoveApp app;
-    private SimpleDoubleProperty doubleProperty = new SimpleDoubleProperty(-1);
+    private SimpleDoubleProperty doubleProperty = new SimpleDoubleProperty(0);
 
     public WorkerValidParam(SagaFileMoveApp app) {
         this.app = app;
@@ -25,13 +25,22 @@ public class WorkerValidParam extends Thread {
 
     @Override
     public void run() {
+        int num = 1;
+        long sleepTime = 200;
+        Double nextSleepTime = 200.0;
         while("".equals(app.getTips())) {
+            setDoubleProperty(num++ / 100.0);
             try {
-                Thread.sleep(500);
+                Thread.sleep(sleepTime);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
+            nextSleepTime = nextSleepTime * 1.06;
+            sleepTime = nextSleepTime.longValue();
+
+            if(num == 101) {
+                break;
+            }
         }
-        setDoubleProperty(1.0);
     }
 }