JavaFX WebEngine停留在“正在运行”状态州 [英] JavaFX WebEngine stuck in "Running" state

查看:842
本文介绍了JavaFX WebEngine停留在“正在运行”状态州的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFX2中使用 WebEngine 时,我注意到它有时会卡住。假设我正在创建一个爬虫,它只是在页面上找到超链接,然后访问它们以递归方式执行相同操作,跟踪我们访问过哪些链接以及哪些链接已经在边界上。在运行我的代码时,执行有时会在任意时刻挂起。

While using the WebEngine in JavaFX2, I've noticed it sometimes just gets stuck. Assume I were making a crawler that simply finds hyperlinks on a page and then visits them to do the same recursively, keeping track of which links we have visited and which are already on the frontier. While running my code, the execution would sometimes hang at arbitrary moments.

我已经以<$ c $的监听器的形式向我的项目添加了一些调试代码c> workDoneProperty 和 exceptionProperty 并打印 loadWorker的每次转换 stateProperty 。然后我注意到有时引擎会停止中间加载一个URL(状态停留在 RUNNING 并且没有更多 workDone 更新)。我假设这是因为超时或某事,但我已经停止等待,看看它是否确实在5分钟后超时。

I've added some debug code to my project in the form of listeners to the workDoneProperty and exceptionProperty and by printing every transition of the loadWorker's stateProperty. Then I noticed sometimes the engine would stop mid-loading of a URL (the state is stuck in RUNNING and there are no more workDone updates). I'm assuming this is because of a time out or something, but I've stopped waiting to see if it is indeed a timeout after 5 minutes.

exceptionProperty 似乎没有生成任何事件, webEngine 过渡到 FAILED 取消,它就会停止。我想知道这是否可能是图书馆中的竞争条件,或者可能是我遗漏的东西......有没有人遇到过这个谁知道如何解决这个问题?对我的应用程序而言,引擎不仅仅是随机停止...非常重要...

The exceptionProperty doesn't seem to generate any events nor does the webEngine transition into FAILED or CANCELLED, it just stops. I'm wondering if this is potentially a race condition in the library or maybe there's something I'm missing... Has anyone encountered this who knows how this can be fixed? It's quite important for my app that the engine doesn't just stop randomnly...

编辑:从我的控制台添加输出:

EDIT: added output from my console:

Work done: -1
Engine Load Worker transitioning into state: READY
Work done: 0
Engine Load Worker transitioning into state: SCHEDULED
Engine Load Worker transitioning into state: RUNNING
Work done: 21
Work done: 24
Work done: 24
Work done: 57
Work done: 72
BUILD STOPPED (total time: 9 minutes 32 seconds)


推荐答案

我遇到了同样的问题。
好​​像我在方法中创建了一个本地WebView实例而没有保留对它的硬引用(因此在方法调用结束后 - 它可能是GC编辑的。)

I've encountered the same problem. Seems like it happened when I've created a local "WebView" instance inside a method without keeping a hard reference to it (so after the method call ended - it was probably GC-ed.)

我通过为我的WebView实例使用静态变量修复了问题(我正在JAVAFX线程中初始化 - 否则我得到一个异常)

I fixed the problem by using a static variable for my WebView instance (that I'm initializing in a JAVAFX thread - otherwise I get an exception)

private static WebView webview;
public static void someMethod() {


    try {
        if (webview == null){
            webview = new WebView();
        }
        WebEngine webEngine = webview.getEngine();
        webEngine.getLoadWorker().stateProperty().addListener(
                new ChangeListener<State>() {
                    public void changed(ObservableValue ov, State oldState, State newState) {
                        System.out.println("newState = " + newState);
                        if (newState == State.SUCCEEDED) {
                            System.out.println(webEngine.getLocation());
                        }
                    }
                });
        webEngine.load("http://javafx.com");
    } catch (Exception ex) {
        System.err.print("error " + ex.getMessage());
        ex.printStackTrace();
    }
}

这篇关于JavaFX WebEngine停留在“正在运行”状态州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆