JavaFX2:我可以暂停后台任务/服务吗? [英] JavaFX2: Can I pause a background Task / Service?

查看:502
本文介绍了JavaFX2:我可以暂停后台任务/服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置一个后台服务,从csv文件执行批量加载事务数据。此后台服务将从映射到控制器/演示者类中的方法的菜单项操作启动。



经常,某些数据在csv文件中出现在数据库中找不到主数据,这通常会导致上传阻塞和失败。



在这种情况下,我希望能够让后台服务暂停其处理,并从演示者类调用一个对话框来接受用户输入。用户输入将用于在数据库中添加主行,之后,后台服务应从其已停止的位置(不是从csv文件的开头,而是从导致错误的行)恢复。 p>

这是否可能在JavaFX中实现,也许使用javafx.concurrent API?


$

b $ b

当后台进程遇到需要用户提示输入的情况时,使用 FutureTask Platform.runLater showAndWait JavaFX应用程序线程上的对话框提示。在后台进程中使用 futureTask.get 暂停后台进程,直到用户输入必要的值,这将允许进程继续。






示例代码段



这是此方法的代码的精髓,可以放置在背景的调用方法process:

  String nextText = readLineFromSource(); 
if(MISSING.equals(nextText)){
updateMessage(提示缺少文本);
FutureTask< String> futureTask = new FutureTask(
new MissingTextPrompt()
);
Platform.runLater(futureTask);
nextText = futureTask.get();
}
...
class MissingTextPrompt implements Callable< String> {
private TextField textField;

@Override public String call()throws Exception {
final Stage dialog = new Stage();
dialog.setScene(createDialogScene());
dialog.showAndWait();
return textField.getText();
}
...
}






示例应用程序



我创建了一个小的,完整的示例应用程序来演示此方法。



示例应用程序的输出是:





示例输出说明



读取的没有缺少值的行只是纯褐色。
输入提示值的行具有淡绿色背景。
已读取十四行,后台任务已在第6行已暂停一次,缺少一个值。提示用户输入缺少的值(用户输入 xyzzy ),然后该过程继续直到第14行也丢失,并且后台任务再次暂停,另一个提示对话框正在显示。


I am trying to set up a background service that would perform bulk loading of transaction data from a csv file. This background service would be initiated from a menu item action mapped to a method in the controller/presenter class.

Ever so often, some data turns up in the csv file for which no master data can be found in the database, this would normally cause the upload to choke and fail.

On such occasions, I would like to be able to have the background service pause its processing and invoke a dialog from a presenter class to take in user input. The user input would be used to add a master row in the database, after which the background service should resume from where it had left off (not from the beginning of the csv file, but from the row which caused the error).

Is this possible to achieve in JavaFX, perhaps with the javafx.concurrent API? How would I go about doing this?

解决方案

Solution

When your background process encounters a situation where it requires a user to be prompted for input, use FutureTask executed in Platform.runLater to showAndWait the dialog prompt on the JavaFX application thread. In the background process use futureTask.get to pause the background process until the user has input the necessary values which will allow the process to continue.


Sample Code Snippet

Here is the essence of code for this approach which can be placed inside the call method of your background process:

String nextText = readLineFromSource();
if ("MISSING".equals(nextText)) {
  updateMessage("Prompting for missing text");
  FutureTask<String> futureTask = new FutureTask(
    new MissingTextPrompt()
  );
  Platform.runLater(futureTask);
  nextText = futureTask.get();
}
...
class MissingTextPrompt implements Callable<String> {
  private TextField textField;

  @Override public String call() throws Exception {
    final Stage dialog = new Stage();
    dialog.setScene(createDialogScene());
    dialog.showAndWait();
    return textField.getText();
  }
  ...
}


Sample Application

I created a small, complete sample application to demonstrate this approach.

The output of the sample application is:

Sample Output Explanation

Lines read without missing values are just plain brown. Lines with a prompt value entered have a pale green background. Fourteen lines have been read, the background task has already paused once at the 6th line which was missing a value. The user was prompted for the missing value (to which the user entered xyzzy), then the process continued until line 14 which is also missing and the background task is again paused and another prompt dialog is being displayed.

这篇关于JavaFX2:我可以暂停后台任务/服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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