I am new in JavaFX and I have a question about Services. I am trying to read data from a file and if after 5 seconds my task has not done I want to throw TimeOutException:
public Service<Void> readFile() {
return new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
System.out.println("Trying to read data...");
//read data though out a file;
});
try {
future.get(5, TimeUnit.SECONDS);
}catch (TimeoutException te) {
System.out.println("TIMEOUT!");
throw new TimeoutException();
}
return null;
}
};
}
};
}
and go to onFailed method to restart the service and do it again and again until it won't be successful. The method which starts my task does it after pressing the button. First starts the animation and if it is finished starts Task:
void readingFile(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("loading.fxml"));
Scene scene = startBtn.getScene();
root.translateXProperty().set(scene.getWidth());
parent.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue keyValue = new KeyValue(root.translateXProperty(), 0 , Interpolator.EASE_IN);
KeyFrame keyFrame = new KeyFrame(Duration.millis(100), keyValue);
timeline.getKeyFrames().add(keyFrame);
timeline.setOnFinished(event1 -> {
parent.getChildren().remove(container);
Label label = (Label) root.lookup("#label");
Service<Void> readTask = readFile();
label.textProperty().unbind();
label.textProperty().bind(readTask.messageProperty());
readTask.start();
readTask.setOnFailed(fail -> {
System.out.println("Fail to read file! Retry...");
readTask.restart();
});
readTask.setOnSucceeded(success -> {
System.out.println("Success");
new Thread(writeFile()).start();
});
});
timeline.play();
} catch (IOException e) {
e.printStackTrace();
}
}
but when my task is restarted, it skips read data code and falls to try block right away and all the time after 5 seconds throw TimeOutException. Why does it happen and how can I solve it? Thanks in advance.