Javafx WebEngine:背景工作者到底发生了什么? UI挂起在loadContent(大HTML文档)

时间:2017-01-24 14:27:56

标签: java javafx javafx-webengine

来自WebEngine文档:

  

加载总是发生在后台线程上。启动的方法   在安排后台作业后立即加载返回。追踪   进度和/或取消作业,使用可用的工具实例   getLoadWorker()方法。

我有HTML string我通过WebEngine.loadContent(String)加载到WebView上。这个字符串大约有500万个字符。在Platform.runLater()中运行它(我必须在JavaFX线程中运行它,否则我收到错误)我的UI挂起大约一分钟。

如果我不在Platform.runLater()中运行,我会:

java.lang.IllegalStateException: Not on FX application thread; currentThread = populator
    at com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:236)
    at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:423)
    at javafx.scene.web.WebEngine.checkThread(WebEngine.java:1216)
    at javafx.scene.web.WebEngine.loadContent(WebEngine.java:931)
    at javafx.scene.web.WebEngine.loadContent(WebEngine.java:919)
    ...

我无法通过webEngine.getLoadWorker().getProgress()查询webEngine,也无法通过webEngine.getLoadWorker().cancel()取消,因为我必须再次在挂起的JavaFX线程上运行...

所以我必须等到页面加载,然后先前提交的任何Platform.runLater(()->webEngine.getLoadWorker().getProgress())(在网页加载过程中)都会运行,每次给我1.0 ...

我用来查询Worker的代码:

// WebView
wvIn.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
    class ProgressThread extends Thread {
        private Worker.State loadWorkerState;

        synchronized Worker.State getLoadWorkerState() {
            return loadWorkerState;
        }

        synchronized void setLoadWorkerState(Worker.State loadWorkerState) {
            this.loadWorkerState = loadWorkerState;
        }

        {
            setDaemon(true);
            setName("LoadingWebpageProgressThread");
        }

        public void run() {
            while (true) {
                try {
                    if (getLoadWorkerState() == Worker.State.RUNNING)
                        // piWv ProgressIndicator (WebView loading)
                        Platform.runLater(() -> piWv.setVisible(true));
                    while (getLoadWorkerState() == Worker.State.RUNNING) {
                        Platform.runLater(() -> {
                            piWv.setProgress(wvIn.getEngine().getLoadWorker().getProgress());
                            // TODO delete
                            System.out.println(wvIn.getEngine().getLoadWorker().getProgress());
                        });
                        Thread.sleep(100);
                    }
                    if (getLoadWorkerState() == Worker.State.SUCCEEDED) {
                        Platform.runLater(() -> piWv.setProgress(1d));
                        Thread.sleep(100);
                        Platform.runLater(() -> {
                            piWv.setVisible(false);
                            piWv.setProgress(0d);
                        });
                    }
                    synchronized (this) {
                        wait();
                    }
                } catch (InterruptedException e) {
                }
            }
        }
    };

    final ProgressThread progressThread = new ProgressThread();
    {
        progressThread.start();
    }

    // executed on JavaFX Thread
    @Override
    public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
        if (newValue == State.SUCCEEDED) {
            JSObject window = (JSObject) wvIn.getEngine().executeScript("window");
            window.setMember("controller", mainController);
            progressThread.setLoadWorkerState(newValue);
            progressThread.interrupt();
        } else if (newValue == State.RUNNING) {
            progressThread.setLoadWorkerState(newValue);
            progressThread.interrupt();
        }
        // TODO delete
        System.out.println(oldValue + "->" + newValue);
    }
});

无论如何强制加载后台线程?

JavaFX线程到底发生了什么?它是填充WebView的过程吗?

2 个答案:

答案 0 :(得分:1)

您在问题中显示的HTML文件几乎立即加载到我的计算机上。

在我看来,问题在于你的代码。嗯,这很麻烦。请允许我指出它看起来不是很好,因为它容易deadlock,并且使用例外来实现guarded-blocks的形式。

看起来你并不完全理解events的概念。 除非为了简洁起见以某种方式切断了问题中的代码段,否则ProgressThread实例完全没用。 您已根据需要进行了ChangeListener个调度事件,并在JavaFX应用程序线程上执行回调;只是使用它。

indicator.setVisible(false); // Start hidden
engine.getLoadWorker().progressProperty().addListener((observable, oldValue, newValue) -> {
    indicator.setProgress(newValue.doubleValue());
    indicator.setVisible(indicator.getProgress() != 1D);
});

或者,你可以使用binding的力量:

Worker<Void> worker = engine.getLoadWorker();
indicator.visibleProperty().bind(worker.stateProperty().isEqualTo(Worker.State.RUNNING));
indicator.progressProperty().bind(worker.progressProperty());

对于您的JavaScript代码,我建议您收听文档事件而不是工作状态事件:它们在您的情况下更有意义,并且在页面实际完成渲染时触发它们,而不仅仅是在页面数据完成时下载。

engine.documentProperty().addListener((observable, oldValue, newValue) -> {
    if (newValue == null) {
        return;
    }
    JSObject window = (JSObject)engine.executeScript("window");
    window.setMember("controller", mainController);
});

请注意,在我的机器上,加载过程中进度始终为零。我认为问题在于你的HTML代码:如果你试图加载一些其他资源,比如你现在正在阅读的网页,进度指示器将提供更愉快的体验,在页面内容完成之前多次更新负荷。

答案 1 :(得分:0)

因此...

我最终构建了自己的Http Server来测试load而不是loadContent(注意:它是一个糟糕的服务器):

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.GZIPOutputStream;

public class BadHttpServer {
    private static int request = 0, connection = 0;
    private static final BadHttpServer SINGLETON = new BadHttpServer();
    private ServerSocket serverSocket;
    private String htmlString, formattedDate;
    private byte[] compressedHTML;

    public String getURL() {
        return "http://localhost:60000";
    }

    synchronized byte[] getCompressedHTML() {
        return compressedHTML;
    }

    synchronized void setCompressedHTML(String string) {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(string.length() / 2);
                GZIPOutputStream gos = new GZIPOutputStream(baos);) {
            gos.write(string.getBytes("utf-8"));
            gos.close();
            this.compressedHTML = baos.toByteArray();
            formattedDate = String.format(Locale.US, "%1$ta, %1$td %1$tb %1$tY %1$tH:%1$tM:%1$tS %1$tZ",
                    Calendar.getInstance(TimeZone.getTimeZone("GMT")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private synchronized String getFormattedDate() {
        return formattedDate;
    }

    private BadHttpServer() {
        try {
            serverSocket = new ServerSocket(60000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        new Thread() {
            {
                setDaemon(true);
                setName("serverSocketThread");
            }

            public void run() {
                while (true) {
                    try {
                        new Thread(acceptAndSendHTML(serverSocket.accept())) {
                            {
                                setDaemon(true);
                                setName("acceptThread" + connection++);
                            }
                        }.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();
    }

    protected Runnable acceptAndSendHTML(final Socket socket) {
        return new Runnable() {
            @Override
            public void run() { 
                try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII"));
                        BufferedWriter asciiWriter = new BufferedWriter(
                                new OutputStreamWriter(socket.getOutputStream(), "ASCII"));
                        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream(), 1000)) {
                    while (!socket.isClosed()) {
                        int request = BadHttpServer.request++;
                        // READING
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append(br.readLine() + "\r\n");
                        for (String line = ""; !(line = br.readLine()).isEmpty(); stringBuilder.append(line + "\r\n"))
                            ;
                        // TODO delete
                        System.out.print("Connection " + request + " request: " + stringBuilder);
                        // WRITING
                        byte[] compressedHTML = getCompressedHTML();
                        asciiWriter.write("HTTP/1.1 100 Continue\r\n");
                        asciiWriter.write("\r\n");
                        asciiWriter.write("HTTP/1.1 200 OK\r\n");
                        asciiWriter.write("Date: " + getFormattedDate() + "\r\n");
                        asciiWriter.write("Content-Type: text/html; charset=utf-8\r\n");
                        asciiWriter.write("Content-Encoding: gzip\r\n");
                        asciiWriter.write("Content-Charset: utf-8\r\n");
                        asciiWriter.write("Content-Length: " + compressedHTML.length + "\r\n");
                        // TODO delete
                        System.out.println("Content-Length: " + compressedHTML.length + "\r\n");
                        asciiWriter.write("\r\n");
                        asciiWriter.flush();
                        for (int writtenOverall = 0, writtenThisIteration = 0; writtenOverall < compressedHTML.length; writtenOverall += writtenThisIteration) {
                            bos.write(compressedHTML, writtenOverall,
                                    writtenThisIteration = Math.min(compressedHTML.length - writtenOverall, 1000));
                            Thread.sleep(10); // Bandwidth throttling
                        }
                        bos.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
    }

    public static BadHttpServer getIntance() {
        return SINGLETON;
    }

    public synchronized void setHtmlString(String htmlString) {
        this.htmlString = htmlString;
        setCompressedHTML(htmlString);
    }

    synchronized String getHtmlString() {
        return htmlString;
    }

}

现在,我不是loadContent,而是

BadHttpServer.getIntance().setHtmlString(htmlString);
Platform.runLater(() -> wvIn.getEngine().load(BadHttpServer.getIntance().getURL()));

我做了一些基准测试 - 测量从loadWorker开始RUNNINGSUCCEDED的时间:

via BadHttpServer:
javascript and style after body: 13,5 s
javascript and style before body: 8,6 s

so I continued to test with javascript and style before body.
without javascript injection: 8,5 s

I continued to test with javascript injection.

Next I did with loadContent: 170 s - no UI resposivness during loading
Yep - almost 3 minutes...

So I returned back with BadHttpServer.
Next I tried "throttling the bandwidth" by changing the sleep time.
no sleep: 8,2 s
5 ms sleep: 6,8 s
10 ms sleep: 6,7 s
20 ms sleep: 11,6 s

In these cases the UI was responsive, although it was lagging a bit.
The progressIndicator showed nicely, pausing a bit at 90%.
By increasing the sleep time, the UI was more responsive.

所以,我猜测loadContent方法可能会更好一点,除非我做错了实现它...