JxBrowser can not dispose in JavaFX

939 views Asked by At

I use Tabpane multiple Browser; when I close a tab, I can't dispose the main IPC:

public class JxbrowserDemo extends Application {
    @Override
    public void start(Stage primaryStage) {
        setLevel(Level.ALL);
        TabPane tabPane = new TabPane();
        tabPane.getTabs().add(createBrowserTab());
        tabPane.getTabs().add(createBrowserTab());
        StackPane pane = new StackPane();
        pane.getChildren().add(tabPane);
        Scene scene = new Scene(pane, 500, 400);
        primaryStage.setTitle("JxBrowser: JavaFX - Hello World");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public Tab createBrowserTab() {
        Tab tab = new Tab("baidu");
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        tab.setOnClosed(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                //last tag close throw exception
                //
                browser.dispose();
            }
        });

        tab.setContent(browserView);
        browser.loadURL("www.google.com");
        //tabPane.getTabs().add(tab);
        //browserArrayList.add(browser);
        return tab;
    }

    @Override
    public void stop() throws Exception {
        //dispose here Success
        /*browserArrayList.forEach(t->{
            t.dispose();
        });*/
        super.stop();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Log:

02:42:22 信息: Shutting down event queue...
02:42:22 信息: Shutting down event queue... [DONE]
02:42:22 信息: Shutting down event queue...
02:42:22 信息: Shutting down event queue... [DONE]
02:42:22 信息: Channel has been disconnected: SocketChannel{cid=1, bid=1, type=Render}
02:42:22 信息: Channel is disconnected: SocketChannel{cid=1, bid=1, type=Render}
02:42:22 信息: Shutting down event queue...
02:42:22 信息: Shutting down event queue... [DONE]
02:42:22 信息: Shutting down event queue...
02:42:22 信息: Shutting down event queue... [DONE]
02:42:22 信息: IPC connection has been closed. Connection ID: 466160688
02:42:22 信息: Pending IPC connections: 2
02:42:22 信息: Channel has been disconnected: SocketChannel{cid=1, bid=1, type=Browser}
02:42:22 信息: Channel is disconnected: SocketChannel{cid=1, bid=1, type=Browser}
02:42:22 信息: Stopping IPC...
02:42:22 信息: Stopping main channel...
02:42:22 信息: IPC connection has been closed. Connection ID: 466160864
02:42:22 信息: Pending IPC connections: 1
02:42:22 详细: WRITE: ShutdownMessage{type=Shutdown, uid=175, SocketInfo{cid=0, bid=-1, channelType=Main

Hang up here.

About five minutes later it throws the exception:

02:42:22 详细: WRITE: ShutdownMessage{type=Shutdown, uid=175, SocketInfo{cid=0, bid=-1, channelType=Main}
Exception in thread "JavaFX Application Thread" com.teamdev.jxbrowser.chromium.internal.ipc.IPCException: Failed to execute shutdown post message.
02:44:22 信息: Shutting down event queue...
02:44:22 信息: Shutting down event queue... [DONE]
02:44:22 信息: Shutting down event queue...
02:44:22 信息: Shutting down event queue... [DONE]
02:44:22 信息: Channel has been disconnected: SocketChannel{cid=0, bid=-1, type=Main}
02:44:22 信息: IPC connection has been closed. Connection ID: 466161040
02:44:22 信息: Pending IPC connections: 0
02:44:22 信息: Chromium process exit code 0
2

There are 2 answers

0
Nikita Shvinagir On

Try disposing Browser instances in non- JavaFx Application Thread:

        tab.setOnClosed(new EventHandler<Event>() {
        @Override
        public void handle(Event event) {
            //last tag close throw exception
            //
            new Thread(new Runnable() {
                @Override
                public void run() {
                    browser.dispose();
                }
            }).start();
        }
    });
0
GOXR3PLUS On

You have to detect the operating system based on this JXBrowser article and then close it in an appropriate way https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013069-lightweight-or-heavyweight

My strategy :

 public class Util {

    public enum OS {
        WINDOWS, LINUX, MAC, SOLARIS
    }// Operating systems.

    private static OS os = null;
    private static final String OPERATING_SYSTEM = System.getProperty("os.name").toLowerCase();

    public static OS getOS() {
        if (os == null) {
            if (OPERATING_SYSTEM.contains("win"))
                os = OS.WINDOWS;
            else if (OPERATING_SYSTEM.contains("nix") || OPERATING_SYSTEM.contains("nux") || OPERATING_SYSTEM.contains("aix")) {
                os = OS.LINUX;
            } else if (OPERATING_SYSTEM.contains("mac"))
                os = OS.MAC;
            else if (OPERATING_SYSTEM.contains("sunos"))
                os = OS.SOLARIS;
        }
        return os;
    }

    public static boolean isWindows() {
        return OPERATING_SYSTEM.contains("win");
    }

    public static boolean isLinux() {
        return ( OPERATING_SYSTEM.contains("nix") || OPERATING_SYSTEM.contains("nux") || OPERATING_SYSTEM.contains("aix") );
    }

    public static boolean isMac() {
        return OPERATING_SYSTEM.contains("mac");
    }

    public static boolean isSolaris() {
        return OPERATING_SYSTEM.contains("sunos");
    }

    /**
     * Exit Application
     * 
     * @param code
     */
    public static void exitApplication(int code) {

        System.out.println("Dis All->" + Util.getOS());
        switch (Util.getOS()) {
            case WINDOWS:
                new Thread(() -> {

                    //->dispose all the browsers
                    code....

                    System.exit(code);
                }).start();
                break;
            case LINUX:
            case MAC:
                Platform.runLater(() -> {
                     //->dispose all the browsers
                     code.....

                    System.exit(code);
                });
                break;
            default:
                System.out.println("Can't dispose browser instance!!!");
                break;
        }

    }
}