0

在操作时,我向 jms 主题发送消息以处理数据,并且我有一个回调方法,当数据准备好并加载 TableView 时会调用该方法。

public void onEnter(ActionEvent actionEvent) throws IOException, InterruptedException {
            new Thread() {
                public void run() {
                    Platform.runLater(() -> {
                        progressIndicator.setVisible(true);
                        scrollPane.setDisable(true);
                    });


                    //  Construct the message and publish it to a topic

                };
            }.start();

        } 
    }



public void callBackMethod(List<Object>  list )  {

        progressIndicator.setVisible(false);
        scrollPane.setDisable(false);
    //load data in the table
}

这就是我想要的,但是如果消息传递系统端出现问题怎么办,回调永远不会被调用,并且 UI 组件将永远被禁用。

任何改进这一点的建议都会有所帮助。

4

1 回答 1

2

据推测,如果消息传递系统无法发送消息,它会抛出某种异常,因此您需要一种方法来捕获并正确恢复。如果您使用 JavaFX“任务”类,那么当发生这种情况时您会收到事件。如果合适的话,您仍然必须处理接收端的故障,或者实施某种超时。

此外,您正在启动一个线程,然后立即使用 RunLater 将作业扔到 FXAT 上。根据定义,onEnter 事件处理程序已经在 FXAT 上运行,因此您可以在启动线程(或任务,正如我建议的那样)之前执行您的 GUI 工作。这是一个示例,它显示了如何启动任务,并在它因异常而失败时进行清理:

public class SampleTask extends Application {

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

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World!");

    BorderPane root = new BorderPane();
    ProgressIndicator progressIndicator = new ProgressIndicator(0);
    ScrollPane scrollPane = new ScrollPane();
    Button button = new Button("Start");
    root.setTop(progressIndicator);
    root.setCenter(scrollPane);
    progressIndicator.setVisible(false);
    root.setBottom(button);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();

    button.setOnAction(actionEvent -> {
        progressIndicator.setVisible(true);
        scrollPane.setDisable(true);
        Task<Void> testTask = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                // Send the message
                return null;
            }
        };
        testTask.setOnFailed(event -> {
            progressIndicator.setVisible(false);
            scrollPane.setDisable(false);
        });
        new Thread(testTask).start();
    });
}

}

于 2016-04-05T02:28:30.217 回答