With JavaFX UI controller event I want to pause the background Service, service which start with that UI controller (as show on following code).
I have found a similar post JavaFX2: Can I pause a background Task / Service. But with that post it will give solution to pause a Service with it's own events, not for events fire with external UI Controller.
Actually in this case it seems I have to set the Service's internal State to make it pause but Worker Interface didn't contains such a Sate. As a example we can force to fail a Service, with it's FAILED State etc.
Thanks.
UI Controller class
public class CommandController implements Initializable {
private CommandService commandService;
private ExecutorService sequentialServiceExecutor;
private List<IOCommandService> servicePool = new ArrayList<>();
@Override
public void initialize(URL location, Resources resources) {
doProceedInitialSteps();
}
private void doProceedInitialSteps() {
// Select available IOCommands
List<IOCommand> commandList = commandService.getCommands();
// Initialised ExecutorService on sequential manner
sequentialServiceExecutor = Executors.newFixedThreadPool(
1,
new ServiceThreadFactory()
);
for (final IOCommand command : commandList) {
IOCommandService service = new IOCommandService(command, this);
service.setExecutor(sequentialServiceExecutor);
service.setOnRunning(new EventHandler<IOCommand>() { }
service.setOnSucceeded(new EventHandler<IOCommand>() { }
service.setOnFailed(new EventHandler<IOCommand>() { }
service.start();
servicePool.add(service);
}
}
@FXML
public void onCancel() {
// TODO: Need to pause current executing IOCommandService until receive the user response from the DialogBox
// Unless pause current executing IOCommandService, that will over lap this dialog box with IOCommandService related dialog boxes etc
Dialogs.DialogResponse response = Dialogs.showWarningDialog();
if(response.toString.equals("OK") {
}
}
}
Service class
public class IOCommandService extends Service<IOCommand> {
private IOCommand command;
private CommandController controller;
public IOCommandService (IOCommand command, CommandController controller) {
this.command = command;
this.controller = controller;
}
@Override
protected Task<IOCommand> createTask() {
return new Task<IOCommand>() {
@Override
protected Integer call() throws Exception {
// Execute the IO command by,
// 1) updating some UI components (label, images etc) on CommandController
// 2) make enable popup some Dialog boxes for get user response
return command;
}
}
}
}