在操作中,我向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组件将永远被禁用。
任何改进的建议都会有所帮助。
如果消息传递系统发送消息失败,可能会引发某种异常,因此您需要一种方法来捕捉并正确恢复。如果您使用JavaFX"Task"类,那么当这种情况发生时,您将获得事件。如果合适的话,你仍然需要处理接收端的故障,或者实现某种超时。
此外,您正在启动一个线程,然后立即用RunLater将作业抛到FXAT上。根据定义,onEnter事件处理程序已经在FXAT上运行,因此您可以在启动线程(或者我建议的Task)之前完成GUI工作。以下是一个示例,展示了如何启动Task,并在出现异常时进行清理:
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();
});
}
}